#######################################
## DR in an Era of Polarization      ## 
## JOP Replication                   ##
#######################################

## Load Packages ----
library(tidyverse)
library(lfe) # for felm
library(lmtest) # for coeftest 
library(stargazer) # for tables 
library(sandwich) # robust SEs
library(broom) # tidy tibbles
library(scales) # rescaling variables 
library(kableExtra) # tables 
library(ggplot2) # plots 
library(weights) # weighted histogram 
library(panelView) # some coefplots
library(dotwhisker) # some coefplots 
library(GGally) # adds to ggplot
library(ggeffects) # for marginal effects
library(ggpubr) # for ggarrange
library(grid) # for ggarrange 
library(reshape2) # for melt 
library(ggh4x) #for facet
library(RColorBrewer) # for color in descriptive table
library(gridExtra) # tables 
library(ggrepel) # multiple panels 
library(estimatr) # SEs 

## Load data (even file) ----

df_even <- read.csv("./mc_cces_evens_final2.csv")

# Set up data for regressions with FEs 
df_even2 <- df_even %>% 
  #fix state_dist for 11-1 
  mutate(state_dist==case_when(st==11 & dist==1 ~ "11-1",
                               TRUE ~ state_dist)) %>%
  # need factor variables 
  mutate(cong = as.factor(cong),
         state_dist = as.factor(state_dist),
         inc_tercile_ideo1 = as.factor(inc_tercile_ideo1),
         inc_tercile_ideo2 = as.factor(inc_tercile_ideo2),
         inc_tercile_pol = as.factor(inc_tercile_pol),
         inc_tercile_pol.wt = as.factor(inc_tercile_pol.wt),
         # rescaling seniority to 0-1 for interpretability 
         gender_congruence.fac = as.factor(gender_congruence),
         race_congruence.fac = as.factor(race_congruence),
         black_mc_nw.fac = as.factor(black_mc_nw),
         hispanic_mc_nw.fac = as.factor(hispanic_mc_nw),
         asian_mc_nw.fac = as.factor(asian_mc_nw),
         mc_seniority = rescale(mc_seniority, to =c(0,1))) %>% 
  group_by(state_dist, mc_party) %>% 
  # district by MC party FE 
  mutate(dist_party_mc = cur_group_id()) %>% 
  ungroup() 

# Set up data subsets of respondents 
# Female Dems 
df_even_fd <- df_even2 %>%
  filter(party7 == 1 & gender==1)
# Male Dems 
df_even_md <- df_even2 %>%
  filter(party7 == 1 & gender==0)
# Female GOP
df_even_fr <- df_even2 %>%
  filter(party7 == 3 & gender==1)
# Male GOP 
df_even_mr <- df_even2 %>%
  filter(party7 == 3 & gender==0)
# Nonwhite Dems 
df_even_nwd <- df_even2 %>%
  filter(party7 == 1 & nonwhite==1)
# White Dems 
df_even_wd <- df_even2 %>%
  filter(party7 == 1 & nonwhite==0)
# Nonwhite GOP
df_even_nwr <- df_even2 %>%
  filter(party7 == 3 & nonwhite==1)
# White GOP 
df_even_wr <- df_even2 %>%
  filter(party7 == 3 & nonwhite==0)
# Black Dems 
df_even_bd <- df_even2 %>%
  filter(party7 == 1 & black==1)
# Black GOP 
df_even_br <- df_even2 %>%
  filter(party7 == 3 & black==1)
# Hispanic Dems 
df_even_hd <- df_even2 %>%
  filter(party7 == 1 & hispanic==1)
# Hispanic GOP 
df_even_hr <- df_even2 %>%
  filter(party7 == 3 & hispanic==1)
# Asian Dems 
df_even_ad <- df_even2 %>%
  filter(party7 == 1 & asian==1)
# Asian GOP 
df_even_ar <- df_even2 %>%
  filter(party7 == 3 & asian==1)

#
## Table 1 ----
# Basic approval for gender ----

# regressing approval on gender congruence with controls for each subgroup and twfe's

# Female D respondents 
mod2 <- felm(approval_rep ~ gender_congruence + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_fd, weights = df_even_fd$weight)
# Male D respondents 
mod3 <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_md, weights = df_even_md$weight)
# Female R respondents 
mod4 <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_fr, weights = df_even_fr$weight)
# Male R respondents 
mod5 <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_mr, weights = df_even_mr$weight)

# Calculate number of treated districts in each dataset
treat2 <- df_even_fd %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod2_t <- length(which(treat2$treated==1))

treat3 <- df_even_md %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod3_t <- length(which(treat3$treated==1))

treat4 <- df_even_fr %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod4_t <- length(which(treat4$treated==1))

treat5 <- df_even_mr %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod5_t <- length(which(treat5$treated==1))

# Put together into table for overleaf (latex)
# Note that I manually make some stylistic edits in overleaf file to merge gender and race tables, but latex code initially from this and no numbers changed
stargazer(mod2, mod3, mod4, mod5,
          # For main paper, not includsing covariates, but have these in appendix
          omit = c('[i][n][d]', "nonwhite_mc", "mc_seniority"), type='latex', header=FALSE, style="ajps",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{(1) D Women \\\\ Respondents}", "\\shortstack{(2) D Men \\\\ Respondents}", "\\shortstack{(3) R Women \\\\ Respondents}", "\\shortstack{(4) R Men \\\\ Respondents}"),
          covariate.labels=c("Gender Congruence = 1"),# "MC Seniority", "POC MC = 1"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X'),
                         c("N districts w MC gender change", mod2_t, mod3_t, mod4_t, mod5_t)),
          #title = "Effects of MC-Constituent Gender Congruence on MC Approval Ratings",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:gender",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)


# Basic approval for race ----

# regressing approval on racial congruence with controls for each subgroup and twfe's

# All respondents 
# Nonwhite D respondents 
mod2.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
# White D respondents 
mod3.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)
# Nonwhite R respondents 
mod4.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
# White R respondents 
mod5.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wr, weights = df_even_wr$weight)

# Calculate number of treated districts in each dataset
treat2.nw <- df_even_nwd %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod2_t.nw <- length(which(treat2.nw$treated==1))

treat3.nw <- df_even_wd %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod3_t.nw <- length(which(treat3.nw$treated==1))

treat4.nw <- df_even_nwr %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod4_t.nw <- length(which(treat4.nw$treated==1))

treat5.nw <- df_even_wr %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod5_t.nw <- length(which(treat5.nw$treated==1))

stargazer(mod2.nw, mod3.nw, mod4.nw, mod5.nw,
          omit = c('[i][n][d]', "mc_gender", "mc_seniority"), type='latex', header=FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{(1) D POC \\\\ Respondents}", "\\shortstack{(2) D White \\\\ Respondents}", "\\shortstack{(3) R POC \\\\ Respondents}", "\\shortstack{(4) R White \\\\ Respondents}"),
          covariate.labels=c("Racial Congruence = 1"), # "MC Seniority", "Woman MC = 1"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X'),
                         c("N districts w MC race change", mod2_t.nw, mod3_t.nw, mod4_t.nw, mod5_t.nw)),
          #title = "Effects of MC-Constituent Racial Congruence on MC Approval Ratings",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:POC",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)

## Figure 1 ----
# Yearly gender ---- 

# Democratic Women respondent model by year 
mod1.yearly <- felm(approval_rep ~ factor(year) + mc_seniority + nonwhite_mc + gender_congruence:factor(year) | 
                      dist_party_mc | 0 | state_dist, data = df_even_fd, weights = df_even_fd$weight)
# Democratic Men respondent model by year 
mod2.yearly <- felm(approval_rep ~ factor(year) + mc_seniority + nonwhite_mc + gender_congruence:factor(year) | 
                      dist_party_mc | 0 | state_dist, data = df_even_md, weights = df_even_md$weight)
# Republican Women respondent model by year 
mod3.yearly <- felm(approval_rep ~ factor(year) + mc_seniority + nonwhite_mc + gender_congruence:factor(year) | 
                      dist_party_mc | 0 | state_dist, data = df_even_fr, weights = df_even_fr$weight)
# Republican Men respondent model by year 
mod4.yearly <- felm(approval_rep ~ factor(year) + mc_seniority + nonwhite_mc + gender_congruence:factor(year) | 
                      dist_party_mc | 0 | state_dist, data = df_even_mr, weights = df_even_mr$weight)

# create data frames with regression output of interest
mod1.yearly_df <- broom::tidy(mod1.yearly) %>% 
  filter(term%in%c("factor(year)2008:gender_congruence",
                   "factor(year)2010:gender_congruence",
                   "factor(year)2012:gender_congruence",
                   "factor(year)2014:gender_congruence",
                   "factor(year)2016:gender_congruence",
                   "factor(year)2018:gender_congruence",
                   "factor(year)2020:gender_congruence")) %>% 
  mutate(model = "Women",
         term = case_when(term=="factor(year)2008:gender_congruence"~"2008",
                          term=="factor(year)2010:gender_congruence"~"2010",
                          term=="factor(year)2012:gender_congruence"~"2012",
                          term=="factor(year)2014:gender_congruence"~"2014",
                          term=="factor(year)2016:gender_congruence"~"2016",
                          term=="factor(year)2018:gender_congruence"~"2018",
                          term=="factor(year)2020:gender_congruence"~"2020"))

mod2.yearly_df <- broom::tidy(mod2.yearly) %>% 
  filter(term%in%c("factor(year)2008:gender_congruence",
                   "factor(year)2010:gender_congruence",
                   "factor(year)2012:gender_congruence",
                   "factor(year)2014:gender_congruence",
                   "factor(year)2016:gender_congruence",
                   "factor(year)2018:gender_congruence",
                   "factor(year)2020:gender_congruence")) %>% 
  mutate(model = "Men",
         term = case_when(term=="factor(year)2008:gender_congruence"~"2008",
                          term=="factor(year)2010:gender_congruence"~"2010",
                          term=="factor(year)2012:gender_congruence"~"2012",
                          term=="factor(year)2014:gender_congruence"~"2014",
                          term=="factor(year)2016:gender_congruence"~"2016",
                          term=="factor(year)2018:gender_congruence"~"2018",
                          term=="factor(year)2020:gender_congruence"~"2020"))

mod3.yearly_df <- broom::tidy(mod3.yearly) %>% 
  filter(term%in%c("factor(year)2008:gender_congruence",
                   "factor(year)2010:gender_congruence",
                   "factor(year)2012:gender_congruence",
                   "factor(year)2014:gender_congruence",
                   "factor(year)2016:gender_congruence",
                   "factor(year)2018:gender_congruence",
                   "factor(year)2020:gender_congruence")) %>% 
  mutate(model = "Women",
         term = case_when(term=="factor(year)2008:gender_congruence"~"2008",
                          term=="factor(year)2010:gender_congruence"~"2010",
                          term=="factor(year)2012:gender_congruence"~"2012",
                          term=="factor(year)2014:gender_congruence"~"2014",
                          term=="factor(year)2016:gender_congruence"~"2016",
                          term=="factor(year)2018:gender_congruence"~"2018",
                          term=="factor(year)2020:gender_congruence"~"2020"))

mod4.yearly_df <- broom::tidy(mod4.yearly) %>% 
  filter(term%in%c("factor(year)2008:gender_congruence",
                   "factor(year)2010:gender_congruence",
                   "factor(year)2012:gender_congruence",
                   "factor(year)2014:gender_congruence",
                   "factor(year)2016:gender_congruence",
                   "factor(year)2018:gender_congruence",
                   "factor(year)2020:gender_congruence")) %>% 
  mutate(model = "Men",
         term = case_when(term=="factor(year)2008:gender_congruence"~"2008",
                          term=="factor(year)2010:gender_congruence"~"2010",
                          term=="factor(year)2012:gender_congruence"~"2012",
                          term=="factor(year)2014:gender_congruence"~"2014",
                          term=="factor(year)2016:gender_congruence"~"2016",
                          term=="factor(year)2018:gender_congruence"~"2018",
                          term=="factor(year)2020:gender_congruence"~"2020"))

# create tibble with all regression output
models <- rbind(mod1.yearly_df, mod2.yearly_df)
models2 <- rbind(mod3.yearly_df, mod4.yearly_df)
models <- models %>% mutate(party = "Democrats")
models2 <- models2 %>% mutate(party = "Republicans")

both.models.gender <- rbind(models, models2)

# Yearly race ---- 

# Democratic Nonwhite respondent model by year 
mod1.yearly.nw <- felm(approval_rep ~ factor(year) + mc_seniority + mc_gender + race_congruence:factor(year) | 
                        dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
# Democratic White respondent model by year 
mod2.yearly.nw <- felm(approval_rep ~ factor(year) + mc_seniority + mc_gender + race_congruence:factor(year) | 
                         dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)
# Republican Nonwhite respondent model by year 
mod3.yearly.nw <- felm(approval_rep ~ factor(year) + mc_seniority + mc_gender + race_congruence:factor(year) | 
                         dist_party_mc | 0 | state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
# Republican White respondent model by year 
mod4.yearly.nw <- felm(approval_rep ~ factor(year) + mc_seniority + mc_gender + race_congruence:factor(year) | 
                         dist_party_mc | 0 | state_dist, data = df_even_wr, weights = df_even_wr$weight)

# create data frames with regression output of interest
mod1.yearly_df.nw <- broom::tidy(mod1.yearly.nw) %>% 
  filter(term%in%c("factor(year)2008:race_congruence",
                   "factor(year)2010:race_congruence",
                   "factor(year)2012:race_congruence",
                   "factor(year)2014:race_congruence",
                   "factor(year)2016:race_congruence",
                   "factor(year)2018:race_congruence",
                   "factor(year)2020:race_congruence")) %>% 
  mutate(model = "Nonwhite",
         term = case_when(term=="factor(year)2008:race_congruence"~"2008",
                          term=="factor(year)2010:race_congruence"~"2010",
                          term=="factor(year)2012:race_congruence"~"2012",
                          term=="factor(year)2014:race_congruence"~"2014",
                          term=="factor(year)2016:race_congruence"~"2016",
                          term=="factor(year)2018:race_congruence"~"2018",
                          term=="factor(year)2020:race_congruence"~"2020"))

mod2.yearly_df.nw <- broom::tidy(mod2.yearly.nw) %>% 
  filter(term%in%c("factor(year)2008:race_congruence",
                   "factor(year)2010:race_congruence",
                   "factor(year)2012:race_congruence",
                   "factor(year)2014:race_congruence",
                   "factor(year)2016:race_congruence",
                   "factor(year)2018:race_congruence",
                   "factor(year)2020:race_congruence")) %>% 
  mutate(model = "White",
         term = case_when(term=="factor(year)2008:race_congruence"~"2008",
                          term=="factor(year)2010:race_congruence"~"2010",
                          term=="factor(year)2012:race_congruence"~"2012",
                          term=="factor(year)2014:race_congruence"~"2014",
                          term=="factor(year)2016:race_congruence"~"2016",
                          term=="factor(year)2018:race_congruence"~"2018",
                          term=="factor(year)2020:race_congruence"~"2020"))

mod3.yearly_df.nw <- broom::tidy(mod3.yearly.nw) %>% 
  filter(term%in%c("factor(year)2008:race_congruence",
                   "factor(year)2010:race_congruence",
                   "factor(year)2012:race_congruence",
                   "factor(year)2014:race_congruence",
                   "factor(year)2016:race_congruence",
                   "factor(year)2018:race_congruence",
                   "factor(year)2020:race_congruence")) %>% 
  mutate(model = "Nonwhite",
         term = case_when(term=="factor(year)2008:race_congruence"~"2008",
                          term=="factor(year)2010:race_congruence"~"2010",
                          term=="factor(year)2012:race_congruence"~"2012",
                          term=="factor(year)2014:race_congruence"~"2014",
                          term=="factor(year)2016:race_congruence"~"2016",
                          term=="factor(year)2018:race_congruence"~"2018",
                          term=="factor(year)2020:race_congruence"~"2020"))

mod4.yearly_df.nw <- broom::tidy(mod4.yearly.nw) %>% 
  filter(term%in%c("factor(year)2008:race_congruence",
                   "factor(year)2010:race_congruence",
                   "factor(year)2012:race_congruence",
                   "factor(year)2014:race_congruence",
                   "factor(year)2016:race_congruence",
                   "factor(year)2018:race_congruence",
                   "factor(year)2020:race_congruence")) %>% 
  mutate(model = "White",
         term = case_when(term=="factor(year)2008:race_congruence"~"2008",
                          term=="factor(year)2010:race_congruence"~"2010",
                          term=="factor(year)2012:race_congruence"~"2012",
                          term=="factor(year)2014:race_congruence"~"2014",
                          term=="factor(year)2016:race_congruence"~"2016",
                          term=="factor(year)2018:race_congruence"~"2018",
                          term=="factor(year)2020:race_congruence"~"2020"))

# create tibble with all regression output
models.nw <- rbind(mod1.yearly_df.nw, mod2.yearly_df.nw)
models2.nw <- rbind(mod3.yearly_df.nw, mod4.yearly_df.nw)
models.nw <- models.nw %>% mutate(party = "Democrats")
models2.nw <- models2.nw %>% mutate(party = "Republicans")

both.models.race <- rbind(models.nw, models2.nw)

# Yearly Together ---- 

# combine with gender models after adding MC Gender and MC Race
both.models.gender <- both.models.gender %>% mutate(identity = "Effect of Gender Congruence")
both.models.race <- both.models.race %>% mutate(identity = "Effect of Race Congruence")

# rename nonwhite in yearly models to POC 
both.models.race$model <- ifelse(both.models.race$model=="Nonwhite", "POC", both.models.race$model)

# Just Gender  
gender.yr <- dwplot(both.models.gender,
                    vline = geom_vline(
                      xintercept = 0,
                      colour = "grey60",
                      linetype = 2
                    ),
                    dot_args = list(aes(shape = model), size=2.5),
                    whisker_args = list(aes(color = model))) +
  theme_bw() + ylab("") +
  ggtitle("A. Gender Congruence") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 12, face = "bold", hjust = 0),
        axis.text.y = element_text(size = 10),
        axis.text.x = element_text(size = 10),
        axis.title.y = element_text(size = 12),
        strip.text.x = element_text(size = 12),
        axis.title.x = element_blank(),
        legend.text = element_text(size=12),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10))+
  scale_colour_manual(values = c("darkgrey", "black")) +
  scale_x_continuous(breaks = c(-0.1, -0.05, 0.00, 0.05, 0.1),
                     labels=c("-0.10", "-0.05", "0.00", "0.05", "0.10")) +
  scale_y_discrete(limits=rev) +
  #xlab("Effect of Gender Congruence") +
  xlab("") +
  ylab("Year") +
  coord_flip() +
  guides(shape = guide_legend("model"), 
         colour = guide_legend("model")) +
  facet_grid( ~ party)

# Just Race

race.yr <- dwplot(both.models.race,
                  vline = geom_vline(
                    xintercept = 0,
                    colour = "grey60",
                    linetype = 2
                  ),
                  dot_args = list(aes(shape = model), size=2.5),
                  whisker_args = list(aes(color = model))) +
  theme_bw() + ylab("") +
  ggtitle("B. Racial Congruence") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 12, face = "bold", hjust = 0),
        axis.text.y = element_text(size = 10),
        axis.text.x = element_text(size = 10),
        axis.title.y = element_text(size = 12),
        axis.title.x = element_blank(),
        strip.text.x = element_text(size = 12),
        legend.text = element_text(size=12),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10))+
  scale_colour_manual(values = c("darkgrey", "black")) +
  scale_x_continuous(breaks = c(-0.1, -0.05, 0.00, 0.05, 0.1, 0.15),
                     labels=c("-0.10", "-0.05", "0.00", "0.05", "0.10", "0.15")) +
  scale_shape_manual(values = c("cross", "square")) +
  scale_y_discrete(limits=rev) +
  #xlab("Effect of Racial Congruence") +
  xlab("") +
  ylab("Year") +
  coord_flip() +
  guides(shape = guide_legend("model"), 
         colour = guide_legend("model")) +
  facet_grid( ~ party)

grid.arrange(gender.yr, race.yr, left="Marginal Effect of Identity Congruence on MC Approval")

#
## Figure 2 ----

## Perceived ideology for gender 
mod2_p.ideo.2 <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fd, weights = df_even_fd$weight)
mod3_p.ideo.2 <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_md, weights = df_even_md$weight)
mod4_p.ideo.2 <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fr, weights = df_even_fr$weight)
mod5_p.ideo.2 <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_mr, weights = df_even_mr$weight)

## Perceived ideology for nonwhite 
mod2_p.ideo.nw.2 <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
mod3_p.ideo.nw.2 <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)
mod4_p.ideo.nw.2 <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
mod5_p.ideo.nw.2 <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wr, weights = df_even_wr$weight)

# create data frames with regression output of interest
mod2_pideo.2 <- broom::tidy(mod2_p.ideo.2) %>% filter(term%in%c("gender_congruence.fac0:inc_tercile_ideo23", "gender_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "Democratic Respondents")
mod3_pideo.2 <- broom::tidy(mod3_p.ideo.2) %>% filter(term%in%c("gender_congruence.fac0:inc_tercile_ideo23", "gender_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "Democratic Respondents")
mod4_pideo.2 <- broom::tidy(mod4_p.ideo.2) %>% filter(term%in%c("gender_congruence.fac0:inc_tercile_ideo23", "gender_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "Republican Respondents")
mod5_pideo.2 <- broom::tidy(mod5_p.ideo.2) %>% filter(term%in%c("gender_congruence.fac0:inc_tercile_ideo23", "gender_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "Republican Respondents")
mod2_pideo_nw.2 <- broom::tidy(mod2_p.ideo.nw.2) %>% filter(term%in%c("race_congruence.fac0:inc_tercile_ideo23", "race_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "Democratic Respondents")
mod3_pideo_nw.2 <- broom::tidy(mod3_p.ideo.nw.2) %>% filter(term%in%c("race_congruence.fac0:inc_tercile_ideo23", "race_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "Democratic Respondents")
mod4_pideo_nw.2 <- broom::tidy(mod4_p.ideo.nw.2) %>% filter(term%in%c("race_congruence.fac0:inc_tercile_ideo23", "race_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "Republican Respondents")
mod5_pideo_nw.2 <- broom::tidy(mod5_p.ideo.nw.2) %>% filter(term%in%c("race_congruence.fac0:inc_tercile_ideo23", "race_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "Republican Respondents")

term <- c("Identity Incongruent", "Identity Congruent", "Identity Incongruent", "Identity Congruent")
mod2_pideo.2 <- rbind(mod2_pideo.2) %>% select(-term)
mod2_pideo.2 <- cbind(mod2_pideo.2, term) %>% mutate(party = "Democrats")
mod3_pideo.2 <- rbind(mod3_pideo.2) %>% select(-term)
mod3_pideo.2 <- cbind(mod3_pideo.2, term) %>% mutate(party = "Democrats")
mod4_pideo.2 <- rbind(mod4_pideo.2) %>% select(-term)
mod4_pideo.2 <- cbind(mod4_pideo.2, term) %>% mutate(party = "Republicans")
mod5_pideo.2 <- rbind(mod5_pideo.2) %>% select(-term)
mod5_pideo.2 <- cbind(mod5_pideo.2, term) %>% mutate(party = "Republicans")

mod2_pideo_nw.2 <- rbind(mod2_pideo_nw.2) %>% select(-term)
mod2_pideo_nw.2 <- cbind(mod2_pideo_nw.2, term) %>% mutate(party = "Democrats")
mod3_pideo_nw.2 <- rbind(mod3_pideo_nw.2) %>% select(-term)
mod3_pideo_nw.2 <- cbind(mod3_pideo_nw.2, term) %>% mutate(party = "Democrats")
mod4_pideo_nw.2 <- rbind(mod4_pideo_nw.2) %>% select(-term)
mod4_pideo_nw.2 <- cbind(mod4_pideo_nw.2, term) %>% mutate(party = "Republicans")
mod5_pideo_nw.2 <- rbind(mod5_pideo_nw.2) %>% select(-term)
mod5_pideo_nw.2 <- cbind(mod5_pideo_nw.2, term) %>% mutate(party = "Republicans")

models.gen1.2 <- rbind(mod2_pideo.2, mod4_pideo.2)
models.gen2.2 <- rbind(mod3_pideo.2, mod5_pideo.2)
models.nw1.2 <- rbind(mod2_pideo_nw.2, mod4_pideo_nw.2)
models.nw2.2 <- rbind(mod3_pideo_nw.2, mod5_pideo_nw.2)

# create tibble with all regression output
models.gen1.2 <- models.gen1.2 %>% mutate(identity = "Women Respondents")
models.gen2.2 <- models.gen2.2 %>% mutate(identity = "Men Respondents")
models_gender.2 <- rbind(models.gen1.2, models.gen2.2)%>%
  mutate(type = "Gender Congruence")

models_gender.2$identity <- factor(models_gender.2$identity,
                                   levels = c("Women Respondents",
                                              "Men Respondents"))

models_gender.2$model <- factor(models_gender.2$model,
                                levels = c("Democratic Respondents",
                                           "Republican Respondents"))

models.nw1.2 <- models.nw1.2 %>% mutate(identity = "POC Respondents")
models.nw2.2 <- models.nw2.2 %>% mutate(identity = "White Respondents")
models_race.2 <- rbind(models.nw1.2, models.nw2.2) %>%
  mutate(type = "Racial Congruence")

models_race.2$identity <- factor(models_race.2$identity,
                                 levels = c("POC Respondents",
                                            "White Respondents"))

models_race.2$model <- factor(models_race.2$model,
                              levels = c("Democratic Respondents",
                                         "Republican Respondents"))

both_models.2 <- rbind(models_race.2, models_gender.2)

# text in figure
dat_text <- data.frame(label = c("Democratic whites punish ideologically incongruent POC \n less than they do ideologically incongruent whites."),
                       identity = c("White Respondents"),
                       x = -0.1,
                       y = "Identity Incongruent")

dat_text2 <- data.frame(label = c("Democratic POC punish ideologically incongruent POC \n less than they do ideologically incongruent whites."),
                       identity = c("POC Respondents"),
                       x = -0.1,
                       y = "Identity Incongruent")

dat_text.gen1 <- data.frame(label = c("Democratic men punish ideologically incongruent women \n less than they do ideologically incongruent men."),
                       identity = c("Men Respondents"),
                       x = -0.1,
                       y = "Identity Incongruent")

dat_text.gen1$identity <- factor(dat_text.gen1$identity,
                                 levels = c("Women Respondents",
                                            "Men Respondents"))

dat_text.gen2 <- data.frame(label = c("Republican women punish ideologically incongruent women \n more than they do ideologically incongruent men."),
                            identity = c("Women Respondents"),
                            x = -0.1,
                            y = "Identity Incongruent")

dat_text.gen2$identity <- factor(dat_text.gen2$identity,
                                 levels = c("Women Respondents",
                                            "Men Respondents"))


tab_gender.2<-dwplot(models_gender.2,
                     vline = geom_vline(
                       xintercept = 0,
                       colour = "grey60",
                       linetype = 2
                     ),
                     dot_args = list(aes(shape = model), size=1.5),
                     whisker_args = list(aes(color = model))) +
  theme_bw() + ylab("") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 12, face = "bold"),
        axis.text.y = element_text(size = 10),
        axis.text.x = element_text(size = 10),
        axis.title.y = element_text(size = 12),
        axis.title.x = element_blank(),
        strip.text.x = element_text(size = 12),
        legend.text = element_text(size=10),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10),
        panel.grid.minor = element_blank())+
  scale_colour_manual(values = c("Grey", "black")) +
  scale_y_discrete(limits=rev) +
  ggtitle("Panel B: Gender Congruence")+
  #xlab("Marginal Effect of MC Gender Congruence on Approval") +
  coord_flip() +
  guides(shape = guide_legend("model"), 
         colour = guide_legend("model")) +
  facet_grid( ~ identity) +
  geom_text_repel(data = dat_text.gen2,
                  aes(x = -.376, y = .9,label = label), 
                  size = 3.25,
                  hjust = .25,
                  nudge_x = .3, nudge_y = .5,
                  segment.linetype = 2,
                  segment.color	 = "grey") +
  geom_text_repel(data = dat_text.gen2,
                  aes(x = -.429, y = 1.9,label = label), 
                  size = 3.25,
                  hjust = .25,
                  nudge_x = .354, nudge_y = .5, 
                  segment.linetype = 2,
                  segment.color	 = "grey") +
  geom_text_repel(data = dat_text.gen1,
                  aes(x = -.331, y = 1.1,label = label), 
                  size = 3.25,
                  hjust = .25,
                  nudge_x = .25, nudge_y = .5,
                  segment.linetype = 2,
                  segment.color	 = "grey") +
  geom_text_repel(data = dat_text.gen1,
                  aes(x = -.362, y = 2.1,label = label), 
                  size = 3.25,
                  hjust = .25,
                  nudge_x = .28, nudge_y = .5, 
                  segment.linetype = 2,
                  segment.color	 = "grey")

tab_race.2<-dwplot(models_race.2,
                   vline = geom_vline(
                     xintercept = 0,
                     colour = "grey60",
                     linetype = 2
                   ),
                   dot_args = list(aes(shape = model), size=1.5),
                   whisker_args = list(aes(color = model))) +
  theme_bw() + ylab("") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 12, face = "bold"),
        axis.text.y = element_text(size = 10),
        axis.text.x = element_text(size = 10),
        axis.title.y = element_text(size = 12),
        axis.title.x = element_blank(),
        strip.text.x = element_text(size = 12),
        legend.text = element_text(size=10),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10),
        panel.grid.minor = element_blank())+
  scale_colour_manual(values = c("Grey", "black")) +
  scale_y_discrete(limits=rev) +
  ggtitle("Panel A: Racial Congruence")+
  #xlab("Marginal Effect of MC Racial Congruence on Approval") +
  coord_flip() +
  guides(shape = guide_legend("model"), 
         colour = guide_legend("model")) +
  facet_grid( ~ identity) +
  geom_text_repel(data = dat_text,
                  aes(x = -.332, y = 1.1,label = label), 
                  size = 3.25,
                  hjust = .25,
                  nudge_x = .25, nudge_y = .5,
                  segment.linetype = 2,
                  segment.color	 = "grey") +
  geom_text_repel(data = dat_text,
                  aes(x = -.381, y = 2.1,label = label), 
                  size = 3.25,
                  hjust = .25,
                  nudge_x = .3, nudge_y = .5, 
                  segment.linetype = 2,
                  segment.color	 = "grey") +
  geom_text_repel(data = dat_text2,
                  aes(x = -.288, y = 1.1,label = label), 
                  size = 3.25,
                  hjust = .25,
                  nudge_x = .23, nudge_y = .5,
                  segment.linetype = 2,
                  segment.color	 = "grey") +
  geom_text_repel(data = dat_text2,
                  aes(x = -.158, y = 2.1,label = label), 
                  size = 3.25,
                  hjust = .25,
                  nudge_x = .1, nudge_y = .5, 
                  segment.linetype = 2,
                  segment.color	 = "grey")

grid.arrange(tab_race.2, tab_gender.2, left = "Marginal Effect of Ideological Incongruence on MC Approval")
# For text to align properly, need to adjust viewer panel properly


 #
## Table 2 ----

# Running full tables here but only include heterogeneous treatment effects in table 2
  # Edits manually made in overleaf and rest included in appendix

# interact Racial congruence and rr with district-level controls for White Dems 
mod_rr1_wd <- felm(approval_rep ~ nonwhite_mc + rr_index:nonwhite_mc + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data=df_even_wd, weights=df_even_wd$weight)
# interact Racial congruence and rr with district-level controls for POC Dems 
mod_rr2_nwd <- felm(approval_rep ~ nonwhite_mc + rr_index:nonwhite_mc + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data=df_even_nwd, weights=df_even_nwd$weight)
# interact Racial congruence and rr with district-level controls for White GOP 
mod_rr3_wr <- felm(approval_rep ~ nonwhite_mc + rr_index:nonwhite_mc + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data=df_even_wr, weights=df_even_wr$weight)
# interact Racial congruence and rr with district-level controls for POC GOP 
mod_rr4_nwr <- felm(approval_rep ~ nonwhite_mc + rr_index:nonwhite_mc + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data=df_even_nwr, weights=df_even_nwr$weight)

# table
stargazer(mod_rr1_wd, mod_rr2_nwd, mod_rr3_wr, mod_rr4_nwr, type="latex", 
          column.labels=c("\\shortstack{Democrat \\\\ White}", "\\shortstack{Democrat \\\\ POC}", "\\shortstack{Republican \\\\ White}", "\\shortstack{Republican \\\\ POC}"), 
          model.numbers=F, style="apsr",
          header=F, title = "Effects of Racial Congruence on MC Approval among D and R Respondents, Interacting MC Race with Racial Resentment",
          covariate.labels=c("POC MC = 1","MC Gender","MC Seniority", "RR | POC MC = 1"),
          add.lines = list(c("District * MC Party FEs", "Y", "Y", "Y", "Y"),
                           c("Congressional session FEs", "Y", "Y", "Y", "Y")), 
          dep.var.labels=c("MC approval"), font.size="small", omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          column.sep.width = "-3pt", 
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(0.1, .05, .01, .001),
          notes.align = "l",
          #label = "tab:dems-rr",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01; ***p < 0.001"), notes.append = F)

# interact gender congruence and hs with district-level controls for White Dems 
mod_hs1_fd <- felm(approval_rep ~ mc_gender + hs_index:mc_gender + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data=df_even_fd, weights=df_even_fd$weight)

# interact gender congruence and hs with district-level controls for POC Dems 
mod_hs2_md <- felm(approval_rep ~ mc_gender + hs_index:mc_gender + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data=df_even_md, weights=df_even_md$weight)

# interact gender congruence and hs with district-level controls for White GOP 
mod_hs3_fr <- felm(approval_rep ~ mc_gender + hs_index:mc_gender + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data=df_even_fr, weights=df_even_fr$weight)

# interact gender congruence and hs with district-level controls for POC GOP 
mod_hs4_mr <- felm(approval_rep ~ mc_gender + hs_index:mc_gender + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data=df_even_mr, weights=df_even_mr$weight)


# table
stargazer(mod_hs2_md, mod_hs1_fd, mod_hs4_mr, mod_hs3_fr, type="latex", 
          column.labels=c("\\shortstack{Democrat \\\\ Man}", "\\shortstack{Democrat \\\\ Woman}", "\\shortstack{Republican \\\\ Man}", "\\shortstack{Republican \\\\ Woman}"), 
          model.numbers=F, style="apsr",
          header=F, title = "Effects of Gender Congruence on MC Approval among D and R Respondents, Interacting MC Gender with Hostile Sexism",
          covariate.labels=c("Woman MC = 1","MC Seniority","POC MC", "HS | Woman MC = 1"),
          add.lines = list(c("District * MC Party FEs", "Y", "Y", "Y", "Y"),
                           c("Congressional session FEs", "Y", "Y", "Y", "Y")), 
          dep.var.labels=c("MC approval"), font.size="small", omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          column.sep.width = "-3pt", 
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(0.1, .05, .01, .001),
          notes.align = "l",
          #label = "tab:dems-rr",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01; ***p < 0.001"), notes.append = F)

#### APPENDIX ----
## A. Comparison with Descriptive Representation Papers ----

papers <- c("This Paper", "Sigelman et al. (1995)", "Gay (2002)", "Lawless (2004)", "Jones (2014)", "Ansolabehere and\n Fraga (2016)", 
            "Hayes and\n Hibbing (2017)", "Costa and\n Schaffner (2018)","Kaslovsky and\n Rogowski (2021)", 
            "Henderson et al. (2021)")
gender <- c("X", "", "", "X", "X","", "", "X", "X", "X")
race <- c("X", "X", "X", "", "","X", "X", "", "", "X")
ideo_cong <- c("X", "X", "X", "", "","", "", "X", "", "")
p.ideo_cong <- c("X", "X", "", "", "","X", "", "", "", "")
causal <- c("X", "X", "", "", "","", "X", "X", "X")
sitting <- c("X", "", "X", "X", "X","X", "", "X", "X", "")
years <- c("2008-2020", "", "1980-1998", "1980-1998", "2006-2010", "2008-2010", "", "2010-2014", "2008-2018", "")

similar_pap <- cbind(papers, gender, race, ideo_cong, p.ideo_cong, causal, sitting, years)
colnames(similar_pap) <- c("Paper", "Gender", "Race", "Ideology", "Perceived Ideology", "Causal", "Sitting Politicians", "Years Covered")

similar_pap <- as.matrix(similar_pap)

stargazer(similar_pap, omit = '[i][n][d]', type = "latex", header=FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE,
          title="Similar Papers", 
          notes.align = "l") 

## B. Descriptive Statistics ----
# Number of CCES Respondents by Party and Congressional Session ----
cong_num <- as.matrix(table(df_even2$cong))
cong_num2 <- cbind(cong_num[,1])
colnames(cong_num2) <- c("N")
rownames(cong_num2) <- c("110th Congress", "111th Congress","112th Congress",
                         "113th Congress","114th Congress","115th Congress",
                         "116th Congress")

rparty_num <- as.matrix(table(df_even2$cong, df_even2$party7))
colnames(rparty_num) <- c("Democratic Respondents", "Independent/Other Respondents", "Republican Respondents")
rownames(rparty_num) <- c("110th Congress", "111th Congress","112th Congress",
                          "113th Congress","114th Congress","115th Congress", "116th Congress")
rpct_D <- as.vector((rparty_num[,1]/(rparty_num[,1] + rparty_num[,2] + rparty_num[,3]))*100)
rpct_Ind <- as.vector((rparty_num[,2]/(rparty_num[,1] + rparty_num[,2]+ rparty_num[,3]))*100)
rpct_R <- as.vector((rparty_num[,3]/(rparty_num[,1] + rparty_num[,2]+ rparty_num[,3]))*100)
rparty_num2 <- cbind(rparty_num[,1], rpct_D, rparty_num[,2], rpct_Ind, rparty_num[,3], rpct_R)
colnames(rparty_num2) <- c("Dem.", "Pct D", "Ind./Other", "Pct I",
                           "Rep.", "Pct R")

cong_party <- cbind(cong_num2,rparty_num2)
stargazer(cong_party, type = "latex", title="Number of CCES Respondents by Party", digits=2, header=F,
          notes.align = "l", style = "apsr")

# Distribution of identity congruence with respect to individual's MC ----
gen_cong <- as.matrix(table(df_even2$gender_congruence))
race_cong <- as.matrix(table(df_even2$race_congruence))
gen_cong_pct <- c(((gen_cong[1]/(gen_cong[1]+gen_cong[2]))*100), ((gen_cong[2]/(gen_cong[1]+gen_cong[2]))*100))
race_cong_pct <- c(((race_cong[1]/(race_cong[1]+race_cong[2]))*100), ((race_cong[2]/(race_cong[1]+race_cong[2]))*100))

cong1_distribution <- cbind(gen_cong, round(gen_cong_pct,2), race_cong, round(race_cong_pct,2))
colnames(cong1_distribution) <- c("Gender","Pct. Gender", "Race", "Pct. Race")
rownames(cong1_distribution) <- c("MC-Respondent Incongruence", "MC-Respondent Congruence")

stargazer(cong1_distribution, type = "latex", 
          title="Distribution of Identity Congruence with Respect to an Individual's MC", digits=2, header=F,
          notes.align = "l", style="apsr")

# Distribution of ideological congruence with respect to individual's MC ----

ideo_inc <- as.matrix(table(df_even2$inc_tercile_ideo1))
ideo_inc.p <- as.matrix(table(df_even2$inc_tercile_ideo2))
ideo_inc_pct <- c(((ideo_inc[1]/(ideo_inc[1]+ideo_inc[2]+ideo_inc[3]))*100), ((ideo_inc[2]/(ideo_inc[1]+ideo_inc[2]+ideo_inc[3]))*100), ((ideo_inc[3]/(ideo_inc[1]+ideo_inc[2]+ideo_inc[3]))*100))
ideo_inc.p_pct <- c(((ideo_inc.p[1]/(ideo_inc.p[1]+ideo_inc.p[2]+ideo_inc.p[3]))*100), ((ideo_inc.p[2]/(ideo_inc.p[1]+ideo_inc.p[2]+ideo_inc.p[3]))*100), ((ideo_inc.p[3]/(ideo_inc.p[1]+ideo_inc.p[2]+ideo_inc.p[3]))*100))

inc2_distribution <- cbind(ideo_inc, round(ideo_inc_pct,2), ideo_inc.p, round(ideo_inc.p_pct,2))
colnames(inc2_distribution) <- c("Ideology","Ideo pct.", "Perceived Ideology", "Perceived Ideo pct.")
rownames(inc2_distribution) <- c("Congruence", "Moderate Congruence", "Incongruence")

stargazer(inc2_distribution, type = "latex", 
          title="Distribution of Ideological Congruence with Respect to an Individual's MC", digits=2, header=F,
          notes.align = "l",style="apsr")

# Number of CCES Respondents by Race of MC ----
bl_num <- as.matrix(table(df_even2$cong, df_even2$black_mc_nw))
pct_bl <- as.vector((bl_num[,2]/(bl_num[,1] + bl_num[,2]))*100)
bl_num2 <- cbind(bl_num[,2], pct_bl)
colnames(bl_num2) <- c("Black", "Pct Black")

h_num <- as.matrix(table(df_even2$cong, df_even2$hispanic_mc_nw))
pct_h <- as.vector((h_num[,2]/(h_num[,1] + h_num[,2]))*100)
h_num2 <- cbind(h_num[,2], pct_h)
colnames(h_num2) <- c("Hisp.", "Pct Hisp.")

a_num <- as.matrix(table(df_even2$cong, df_even2$asian_mc_nw))
pct_a <- as.vector((a_num[,2]/(a_num[,1] + a_num[,2]))*100)
a_num2 <- cbind(a_num[,2], pct_a)
colnames(a_num2) <- c("Asian", "Pct Asian")

w_num <- as.matrix(table(df_even2$cong, df_even2$nonwhite_mc))
pct_w <- as.vector((w_num[,1]/(w_num[,1] + w_num[,2]))*100)
w_num2 <- cbind(w_num[,1], pct_w)
colnames(w_num2) <- c("White", "Pct White")

race_cong <- cbind(bl_num2, h_num2, a_num2, w_num2)
rownames(race_cong) <- c("110th Congress", "111th Congress","112th Congress",
                         "113th Congress","114th Congress","115th Congress", "116th Congress")

stargazer(race_cong, type = "latex", title="Number of CCES Respondents by Race of MC", digits=2, header=F,
          notes.align = "l", style="apsr")


# Number of CCES Respondents by Gender of MC ----
gen_num <- as.matrix(table(df_even2$cong, df_even2$mc_gender))
pct_gen <- as.vector((gen_num[,2]/(gen_num[,1] + gen_num[,2]))*100)
pct_men <- as.vector((gen_num[,1]/(gen_num[,1] + gen_num[,2]))*100)
gen_num2 <- cbind(gen_num[,2], pct_gen, gen_num[,1], pct_men)
colnames(gen_num2) <- c("Woman", "Pct Woman", "Men", "Pct Men")

rownames(gen_num2) <- c("110th Congress", "111th Congress","112th Congress",
                        "113th Congress","114th Congress","115th Congress", "116th Congress")

stargazer(gen_num2, type = "latex", title="Number of CCES Respondents by Gender of MC", digits=2, header=F,
          notes.align = "l", style="apsr")

# MC Race and Gender Temporally ----

# First just distribution over time of poc and women MCs 
# Just need percent of POC MCs (out of all) over time and percent of women MCs (out of all) over time 
df.treatment.gen <- df_even2 %>%
  select(year, mc_party, name_full, mc_gender) %>%
  unique() %>%
  filter(!is.na(mc_party)) %>%
  group_by(year, mc_party, mc_gender) %>%
  summarise(total = length(name_full),
            percent = (total/435)*100) %>%
  ungroup() %>%
  mutate(mc_party = case_when(mc_party==1 ~ "Democratic MC",
                              mc_party==0 ~ "Republican MC"))

# keep just women 
df.treatment.gen2 <- df.treatment.gen %>%
  filter(mc_gender==1) %>%
  mutate(mc_gender = case_when(mc_gender==1 ~ "Woman MC")) %>%
  rename(trait = mc_gender)

# for race
df.treatment.race <- df_even2 %>%
  select(year, mc_party, name_full, nonwhite_mc) %>%
  unique() %>%
  filter(!is.na(mc_party)) %>%
  group_by(year, mc_party, nonwhite_mc) %>%
  summarise(total = length(name_full),
            percent = (total/435)*100) %>%
  ungroup() %>%
  mutate(mc_party = case_when(mc_party==1 ~ "Democratic MC",
                              mc_party==0 ~ "Republican MC"))

# keep just POC 
df.treatment.race2 <- df.treatment.race %>%
  filter(nonwhite_mc==1) %>%
  mutate(nonwhite_mc = case_when(nonwhite_mc==1 ~ "POC MC")) %>%
  rename(trait = nonwhite_mc)

# Combine to facet 
df.treatment.total <- rbind(df.treatment.gen2, df.treatment.race2)

ggplot(df.treatment.total, aes(x=year, y=percent, fill=trait)) +
  geom_bar(position = "dodge", stat = "identity") +
  theme_bw() +
  scale_fill_grey()+
  labs(x = "Years", y = "Percent of MCs in Congress who are POC/Women") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
        axis.text.y = element_text(size = 8),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10)) +
  scale_x_continuous(breaks=c(2008, 2010, 2012, 2014, 2016, 2018, 2020)) +
  facet_wrap(~mc_party)

# Percent Incongruence Over time ----
df.treatment.gencong <- df_even2 %>%
  select(year, mc_party, name_full, gender_congruence, mc_gender) %>%
  filter(!is.na(mc_party)) %>%
  group_by(year, mc_party, gender_congruence, mc_gender) %>%
  # number of respondents with congruent mcs by mc gender, party, and year
  summarise(total = length(name_full)) %>%
  ungroup()

df.treatment.gencong2 <- df.treatment.gencong %>% 
  group_by(year) %>%
  summarise(yr.total = sum(total)) %>%
  ungroup() %>%
  left_join(df.treatment.gencong, by="year") %>%
  # want percent constituents w gender congruent mcs by mc gender and party
  group_by(gender_congruence, year, mc_gender) %>%
  summarise(total = total) %>%
  mutate(mc_party = c("Republican MC","Democratic MC")) %>%
  mutate(gender_congruence = case_when(gender_congruence == 0 ~ "Incongruent",
                                       gender_congruence == 1 ~ "Congruent"),
         mc_gender = case_when(mc_gender == 1 ~ "Woman MC",
                               mc_gender == 0 ~ "Man MC"))

ggplot(df.treatment.gencong2, aes(x=year, y=total, fill=gender_congruence)) +
  geom_bar(position = "dodge", stat = "identity") +
  theme_bw() +
  scale_fill_grey()+
  labs(x = "Years", y = "Total Respondents who are Gender Congruent") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
        axis.text.y = element_text(size = 8),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10)) +
  facet_grid(mc_party~mc_gender) +
  scale_x_continuous(breaks=c(2008, 2010, 2012, 2014, 2016, 2018, 2020))

# For Race 

df.treatment.racecong <- df_even2 %>%
  select(year, mc_party, name_full, race_congruence, nonwhite_mc) %>%
  filter(!is.na(mc_party)) %>%
  group_by(year, mc_party, race_congruence, nonwhite_mc) %>%
  # number of respondents with congruent mcs by mc race, party, and year
  summarise(total = length(name_full)) %>%
  ungroup()

df.treatment.racecong2 <- df.treatment.racecong %>% 
  group_by(year) %>%
  summarise(yr.total = sum(total)) %>%
  ungroup() %>%
  left_join(df.treatment.racecong, by="year") %>%
  # want percent constituents w gender congruent mcs by mc gender and party
  group_by(race_congruence, year, nonwhite_mc) %>%
  summarise(total = total) %>%
  mutate(mc_party = c("Republican MC", "Democratic MC")) %>%
  mutate(race_congruence = case_when(race_congruence == 0 ~ "Incongruent",
                                     race_congruence == 1 ~ "Congruent"),
         nonwhite_mc = case_when(nonwhite_mc == 1 ~ "POC MC",
                                 nonwhite_mc == 0 ~ "White MC"))

ggplot(df.treatment.racecong2, aes(x=year, y=total, fill=race_congruence)) +
  geom_bar(position = "dodge", stat = "identity") +
  theme_bw() +
  scale_fill_grey()+
  labs(x = "Years", y = "Percent of Respondents who are Race Congruent") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
        axis.text.y = element_text(size = 8),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10)) +
  facet_grid(mc_party~nonwhite_mc)+
  scale_x_continuous(breaks=c(2008, 2010, 2012, 2014, 2016, 2018, 2020))

# Average Approval by Year ----

# AMONG DEMOCRATIC MEN 
means.yrly <- df_even_md %>%
  select(mc_gender, year, approval_rep) %>%
  filter(!is.na(mc_gender)) %>%
  group_by(mc_gender, year) %>%
  summarise(avg.approval = mean(approval_rep, na.rm=T)) %>%
  ungroup()

# split and put back together to get means for women, men, and difference 
means.yrly.m <- means.yrly %>%
  filter(mc_gender==0) %>%
  rename(men.approval = avg.approval) %>%
  select(-mc_gender)

means.yrly.w <- means.yrly %>%
  filter(mc_gender==1) %>%
  rename(women.approval = avg.approval) %>%
  select(-mc_gender)

means.yrly2 <- means.yrly.m %>%
  left_join(means.yrly.w, by=c("year")) %>%
  mutate(difference = women.approval-men.approval,
         year = as.character(year))

difference <- means.yrly2 %>%
  select(year, difference) %>%
  rename(avg.approval = difference) %>%
  mutate(mc_gender = "Difference")

means.yrly3 <- means.yrly %>%
  mutate(mc_gender = as.character(mc_gender),
         mc_gender = case_when(mc_gender==1 ~ "Woman MC",
                               mc_gender==0 ~ "Man MC")) %>%
  rbind(difference)

ggplot(means.yrly3, aes(x=year, y=avg.approval, group=mc_gender)) +
  geom_line(aes(color=mc_gender)) +
  theme_bw() +
  scale_color_grey()+
  labs(x = "Years", y = "Average approval for MCs among Democratic Men") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
        axis.text.y = element_text(size = 12),
        axis.text.x = element_text(size = 12),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10),
        axis.title = element_text(size = 12),
        legend.text = element_text(size=12)) 

# AMONG DEMOCRATIC WHITES
means.yrly.w <- df_even_wd %>%
  select(nonwhite_mc, year, approval_rep) %>%
  filter(!is.na(nonwhite_mc)) %>%
  group_by(nonwhite_mc, year) %>%
  summarise(avg.approval = mean(approval_rep, na.rm=T)) %>%
  ungroup()

# split and put back together to get means for poc, whites, and difference 
means.yrly.white <- means.yrly.w %>%
  filter(nonwhite_mc==0) %>%
  rename(white.approval = avg.approval) %>%
  select(-nonwhite_mc)

means.yrly.poc <- means.yrly.w %>%
  filter(nonwhite_mc==1) %>%
  rename(poc.approval = avg.approval) %>%
  select(-nonwhite_mc)

means.yrly2 <- means.yrly.white %>%
  left_join(means.yrly.poc, by=c("year")) %>%
  mutate(difference = poc.approval-white.approval,
         year = as.character(year))

difference <- means.yrly2 %>%
  select(year, difference) %>%
  rename(avg.approval = difference) %>%
  mutate(nonwhite_mc = "Difference")

means.yrly3 <- means.yrly.w %>%
  mutate(nonwhite_mc = as.character(nonwhite_mc),
         nonwhite_mc = case_when(nonwhite_mc==1 ~ "POC MC",
                                 nonwhite_mc==0 ~ "White MC")) %>%
  rbind(difference)

ggplot(means.yrly3, aes(x=year, y=avg.approval, group=nonwhite_mc)) +
  geom_line(aes(color=nonwhite_mc)) +
  theme_bw() +
  scale_color_grey()+
  labs(x = "Years", y = "Average approval for MCs among Democratic Whites") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
        axis.text.y = element_text(size = 12),
        axis.text.x = element_text(size = 12),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10),
        axis.title = element_text(size = 12),
        legend.text = element_text(size=12)) 



## C. Disaggregated Racial Groups ----

## BLACK 
# Basic 
mod2.b <- felm(approval_rep ~ black_mc_nw + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                 state_dist, data = df_even_bd, weights = df_even_bd$weight)
mod3.b <- felm(approval_rep ~ black_mc_nw + mc_seniority  + mc_gender | cong + dist_party_mc | 0 | 
                 state_dist, data = df_even_wd, weights = df_even_wd$weight)
## Perceived ideology for Black MCs/Respondents  
mod2_p.ideo.b <- felm(approval_rep ~ black_mc_nw.fac + inc_tercile_ideo2:black_mc_nw.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_bd, weights = df_even_bd$weight)

mod3_p.ideo.b <- felm(approval_rep ~ black_mc_nw.fac + inc_tercile_ideo2:black_mc_nw.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)

# Calculate number of treated districts in each dataset
treat1.b <- df_even_bd %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(black_mc_nw)) %>%
  select(state_dist, black_mc_nw, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_black = mean(black_mc_nw, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_black > 0 & pct_black < 1 ~ 1, TRUE ~ 0))
mod1_t.b <- length(which(treat1.b$treated==1))

treat2.b <- df_even_wd %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(black_mc_nw)) %>%
  select(state_dist, black_mc_nw, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_black = mean(black_mc_nw, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_black > 0 & pct_black < 1 ~ 1, TRUE ~ 0))
mod2_t.b <- length(which(treat2.b$treated==1))

# Democrats 
stargazer(mod2.b, mod2_p.ideo.b, mod3.b, mod3_p.ideo.b,
          omit = '[i][n][d]', type='latex', header=FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{Black \\\\ Respondents}", "\\shortstack{White \\\\ Respondents}"),
          column.separate = c(2,2),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X','X','X'),
                         c("N districts w MC race change", mod1_t.b, mod2_t.b,mod1_t.b, mod2_t.b)),
          title = "Effects of Black MC and Ideological Congruence on MC Approval Ratings (Democratic Respondents)",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:race.ideo.dem",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)

## HISPANIC 
# Basic 
mod2.h <- felm(approval_rep ~ hispanic_mc_nw + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                 state_dist, data = df_even_hd, weights = df_even_hd$weight)
mod3.h <- felm(approval_rep ~ hispanic_mc_nw + mc_seniority  + mc_gender | cong + dist_party_mc | 0 | 
                 state_dist, data = df_even_wd, weights = df_even_wd$weight)

## Perceived ideology for Hispanic MCs/Respondents  
mod2_p.ideo.h <- felm(approval_rep ~ hispanic_mc_nw.fac + inc_tercile_ideo2:hispanic_mc_nw.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_hd, weights = df_even_hd$weight)

mod3_p.ideo.h <- felm(approval_rep ~ hispanic_mc_nw.fac + inc_tercile_ideo2:hispanic_mc_nw.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)

# Calculate number of treated districts in each dataset
treat1.h <- df_even_hd %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(hispanic_mc_nw)) %>%
  select(state_dist, hispanic_mc_nw, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_hisp = mean(hispanic_mc_nw, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_hisp > 0 & pct_hisp < 1 ~ 1, TRUE ~ 0))
mod1_t.h <- length(which(treat1.h$treated==1))

treat2.h <- df_even_wd %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(hispanic_mc_nw)) %>%
  select(state_dist, hispanic_mc_nw, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_hisp = mean(hispanic_mc_nw, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_hisp > 0 & pct_hisp < 1 ~ 1, TRUE ~ 0))
mod2_t.h <- length(which(treat2.h$treated==1))


# Democrats 
stargazer(mod2.h, mod2_p.ideo.h, mod3.h, mod3_p.ideo.h,
          omit = '[i][n][d]', type='latex', header=FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{Hispanic Respondents}", "\\shortstack{White Respondents}"),
          column.separate = c(2,2),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X','X','X'),
                         c("N districts w MC race change", mod1_t.h, mod2_t.h, mod1_t.h, mod2_t.h)),
          title = "Effects of Hispanic MC and Ideological Congruence on MC Approval Ratings (Democratic Respondents)",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:race.ideo.dem",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)

## ASIAN 
# Basic 
mod2.a <- felm(approval_rep ~ asian_mc_nw + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                 state_dist, data = df_even_ad, weights = df_even_ad$weight)
mod3.a <- felm(approval_rep ~ asian_mc_nw + mc_seniority  + mc_gender | cong + dist_party_mc | 0 | 
                 state_dist, data = df_even_wd, weights = df_even_wd$weight)

## Perceived ideology for Asian MCs/Respondents  
mod2_p.ideo.a <- felm(approval_rep ~ asian_mc_nw.fac + inc_tercile_ideo2:asian_mc_nw.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_ad, weights = df_even_ad$weight)

mod3_p.ideo.a <- felm(approval_rep ~ asian_mc_nw.fac + inc_tercile_ideo2:asian_mc_nw.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)


# Calculate number of treated districts in each dataset
treat1.a <- df_even_ad %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(asian_mc_nw)) %>%
  select(state_dist, asian_mc_nw, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_asian = mean(asian_mc_nw, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_asian > 0 & pct_asian < 1 ~ 1, TRUE ~ 0))
mod1_t.a <- length(which(treat1.a$treated==1))

treat2.a <- df_even_wd %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(asian_mc_nw)) %>%
  select(state_dist, asian_mc_nw, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_asian = mean(asian_mc_nw, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_asian > 0 & pct_asian < 1 ~ 1, TRUE ~ 0))
mod2_t.a <- length(which(treat2.a$treated==1))


# Democrats 
stargazer(mod2.a, mod2_p.ideo.a, mod3.a, mod3_p.ideo.a,
          omit = '[i][n][d]', type='text', header=FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{Asian Respondents}", "\\shortstack{White Respondents}"),
          column.separate = c(2,2),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X','X','X'),
                         c("N districts w MC race change", mod1_t.a, mod2_t.a, mod1_t.a, mod2_t.a)),
          title = "Effects of Asian MC and Ideological Congruence on MC Approval Ratings (Democratic Respondents)",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:race.ideo.dem",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)

#
## D. Post-2012 Analysis ---- 

# Generating Tables for the post-2012 data 

# Need to only look after 2012 
df_even2_12_20 <- df_even2 %>%
  filter(year %in% c(2012, 2014, 2016, 2018, 2020))

# Female Dems 
df_even_fd_12_20 <- df_even2_12_20 %>%
  filter(party7 == 1 & gender==1)
# Male Dems 
df_even_md_12_20 <- df_even2_12_20 %>%
  filter(party7 == 1 & gender==0)
# Female GOP
df_even_fr_12_20 <- df_even2_12_20 %>%
  filter(party7 == 3 & gender==1)
# Male GOP 
df_even_mr_12_20 <- df_even2_12_20 %>%
  filter(party7 == 3 & gender==0)
# Nonwhite Dems 
df_even_nwd_12_20 <- df_even2_12_20 %>%
  filter(party7 == 1 & nonwhite==1)
# White Dems 
df_even_wd_12_20 <- df_even2_12_20 %>%
  filter(party7 == 1 & nonwhite==0)
# Nonwhite GOP
df_even_nwr_12_20 <- df_even2_12_20 %>%
  filter(party7 == 3 & nonwhite==1)
# White GOP 
df_even_wr_12_20 <- df_even2_12_20 %>%
  filter(party7 == 3 & nonwhite==0)

# female Democrats 
# Basic model 
mod.fd <- felm(approval_rep ~ gender_congruence + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data=df_even_fd, weights=df_even_fd$weight)
mod12.fd <- felm(approval_rep ~ gender_congruence + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data=df_even_fd_12_20, weights=df_even_fd_12_20$weight)

# + perceived ideological incongruence
mod.2.fd <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fd, weights = df_even_fd$weight)
mod12.2.fd <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fd_12_20, weights = df_even_fd_12_20$weight)

# Combine 
basic.fd <- c(mod.fd$coefficients[[1]],mod.fd$se[[1]], mod12.fd$coefficients[[1]], mod12.fd$se[[1]])
pideo1.fd <- c(mod.2.fd$coefficients[[4]], mod.2.fd$se[[4]], mod12.2.fd$coefficients[[4]], mod12.2.fd$se[[4]])
pideo2.fd <- c(mod.2.fd$coefficients[[5]], mod.2.fd$se[[5]], mod12.2.fd$coefficients[[5]], mod12.2.fd$se[[5]])
pideo3.fd <- c(mod.2.fd$coefficients[[6]], mod.2.fd$se[[6]], mod12.2.fd$coefficients[[6]], mod12.2.fd$se[[6]])
pideo4.fd <- c(mod.2.fd$coefficients[[7]], mod.2.fd$se[[7]], mod12.2.fd$coefficients[[7]], mod12.2.fd$se[[7]])

comparison.fd <- rbind(basic.fd, pideo1.fd, pideo2.fd, pideo3.fd, pideo4.fd)
colnames(comparison.fd) <- c("Full Estimate", "Full SE", "Post-2012 Estimate", "Post-2012 SE")
rownames(comparison.fd) <- c("Gender Congruence", "Ideo. Tercile 1-2, Gender Incongruence", 
                             "Ideo. Tercile 1-3, Gender Incongruence", "Ideo. Tercile 1-2, Gender Congruence", 
                             "Ideo. Tercile 1-3, Gender Congruence")

comparison.fd[,1:4] <- round(comparison.fd[,1:4], 3)

stargazer(comparison.fd, type = "latex", title="Main Gender Models Post-Redistricting (Women Democratic Respondents)", 
          digits=3, header=F,style="apsr",
          colnames = TRUE, rownames = TRUE, 
          column.sep.width = ".5pt", font.size="scriptsize")

# Male Democratic respondents 
# Basic model 
mod.md <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
                 state_dist, data = df_even_md, weights = df_even_md$weight)
mod12.md <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
                   state_dist, data = df_even_md_12_20, weights = df_even_md_12_20$weight)

# + perceived ideological incongruence
mod.3.md <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_md, weights = df_even_md$weight)
mod12.3.md <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_md_12_20, weights = df_even_md_12_20$weight)

# Combine 
basic.md <- c(mod.md$coefficients[[1]],mod.md$se[[1]], mod12.md$coefficients[[1]], mod12.md$se[[1]])
pideo1.md <- c(mod.3.md$coefficients[[4]], mod.3.md$se[[4]], mod12.3.md$coefficients[[4]], mod12.3.md$se[[4]])
pideo2.md <- c(mod.3.md$coefficients[[5]], mod.3.md$se[[5]], mod12.3.md$coefficients[[5]], mod12.3.md$se[[5]])
pideo3.md <- c(mod.3.md$coefficients[[6]], mod.3.md$se[[6]], mod12.3.md$coefficients[[6]], mod12.3.md$se[[6]])
pideo4.md <- c(mod.3.md$coefficients[[7]], mod.3.md$se[[7]], mod12.3.md$coefficients[[7]], mod12.3.md$se[[7]])

comparison.md <- rbind(basic.md, pideo1.md, pideo2.md, pideo3.md, pideo4.md)
colnames(comparison.md) <- c("Full Estimates", "Full SE", "Post-2012 Estimates", "Post-2012 SE")
rownames(comparison.md) <- c("Gender Congruence", "Ideo. Tercile 1-2, Gender Incongruence", 
                             "Ideo. Tercile 1-3, Gender Incongruence", "Ideo. Tercile 1-2, Gender Congruence", 
                             "Ideo. Tercile 1-3, Gender Congruence")

comparison.md[,1:4] <- round(comparison.md[,1:4], 3)

stargazer(comparison.md, type = "latex", title="Main Gender Models Post-Redistricting (Men Democratic Respondents)", 
          digits=3, header=F,style="apsr",
          colnames = TRUE, rownames = TRUE, 
          column.sep.width = ".5pt", font.size="scriptsize")

# Generating Tables for the post-2012 data 
# POC Democrats 
# Basic model 
mod.nwd <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data=df_even_nwd, weights=df_even_nwd$weight)
mod12.nwd <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data=df_even_nwd_12_20, weights=df_even_nwd_12_20$weight)

# + perceived ideological incongruence
mod.3.nwd <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
mod12.3.nwd <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwd_12_20, weights = df_even_nwd_12_20$weight)

# Combine 
basic.nwd <- c(mod.nwd$coefficients[[1]],mod.nwd$se[[1]], mod12.nwd$coefficients[[1]], mod12.nwd$se[[1]])
pideo1.nwd <- c(mod.3.nwd$coefficients[[4]], mod.3.nwd$se[[4]], mod12.3.nwd$coefficients[[4]], mod12.3.nwd$se[[4]])
pideo2.nwd <- c(mod.3.nwd$coefficients[[5]], mod.3.nwd$se[[5]], mod12.3.nwd$coefficients[[5]], mod12.3.nwd$se[[5]])
pideo3.nwd <- c(mod.3.nwd$coefficients[[6]], mod.3.nwd$se[[6]], mod12.3.nwd$coefficients[[6]], mod12.3.nwd$se[[6]])
pideo4.nwd <- c(mod.3.nwd$coefficients[[7]], mod.3.nwd$se[[7]], mod12.3.nwd$coefficients[[7]], mod12.3.nwd$se[[7]])

comparison.nwd <- rbind(basic.nwd, pideo1.nwd, pideo2.nwd, pideo3.nwd, pideo4.nwd)
colnames(comparison.nwd) <- c("Full Estimates", "Full SE", "Post-2012 Estimates", "Post-2012 SE")
rownames(comparison.nwd) <- c("Racial Congruence", "Ideo. Tercile 1-2, Racial Incongruence", 
                              "Ideo. Tercile 1-3, Racial Incongruence", "Ideo. Tercile 1-2, Racial Congruence", 
                              "Ideo. Tercile 1-3, Racial Congruence")

comparison.nwd[,1:4] <- round(comparison.nwd[,1:4], 3)

stargazer(comparison.nwd, type = "latex", title="Main Race Models Post-Redistricting (POC Democratic Respondents)", 
          digits=3, header=F,style="apsr",
          colnames = TRUE, rownames = TRUE, 
          column.sep.width = ".5pt", font.size="scriptsize")

# White Democratic respondents 
# Basic model 
mod.wd <- felm(approval_rep ~ race_congruence + mc_seniority  + mc_gender | cong + dist_party_mc | 0 | 
                 state_dist, data = df_even_wd, weights = df_even_wd$weight)
mod12.wd <- felm(approval_rep ~ race_congruence + mc_seniority  + mc_gender | cong + dist_party_mc | 0 | 
                   state_dist, data = df_even_wd_12_20, weights = df_even_wd_12_20$weight)

# + perceived ideological incongruence
mod.3.wd <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)
mod12.3.wd <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd_12_20, weights = df_even_wd_12_20$weight)

# Combine 
basic.wd <- c(mod.wd$coefficients[[1]],mod.wd$se[[1]], mod12.wd$coefficients[[1]], mod12.wd$se[[1]])
pideo1.wd <- c(mod.3.wd$coefficients[[4]], mod.3.wd$se[[4]], mod12.3.wd$coefficients[[4]], mod12.3.wd$se[[4]])
pideo2.wd <- c(mod.3.wd$coefficients[[5]], mod.3.wd$se[[5]], mod12.3.wd$coefficients[[5]], mod12.3.wd$se[[5]])
pideo3.wd <- c(mod.3.wd$coefficients[[6]], mod.3.wd$se[[6]], mod12.3.wd$coefficients[[6]], mod12.3.wd$se[[6]])
pideo4.wd <- c(mod.3.wd$coefficients[[7]], mod.3.wd$se[[7]], mod12.3.wd$coefficients[[7]], mod12.3.wd$se[[7]])

comparison.wd <- rbind(basic.wd, pideo1.wd, pideo2.wd, pideo3.wd, pideo4.wd)
colnames(comparison.wd) <- c("Full Estimates", "Full SE", "Post-2012 Estimates", "Post-2012 SE")
rownames(comparison.wd) <- c("Racial Congruence", "Ideo. Tercile 1-2, Racial Incongruence", 
                             "Ideo. Tercile 1-3, Racial Incongruence", "Ideo. Tercile 1-2, Racial Congruence", 
                             "Ideo. Tercile 1-3, Racial Congruence")

comparison.wd[,1:4] <- round(comparison.wd[,1:4], 3)

stargazer(comparison.wd, type = "latex", title="Main Race Models Post-Redistricting (White Democratic Respondents)", 
          digits=3, header=F,style="apsr",
          colnames = TRUE, rownames = TRUE, 
          column.sep.width = ".5pt", font.size="scriptsize")

#
## E. Lags and Leads ----

# Basic Gender 
# Female D respondents 
mod1_lag_lead <- felm(approval_rep ~ lag2_gen_congruent + lag_gen_congruent + gender_congruence + lead_gen_congruent + lead2_gen_congruent + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fd, weights = df_even_fd$weight)
# Male D respondents 
mod2_lag_lead <- felm(approval_rep ~ lag2_gen_congruent + lag_gen_congruent + gender_congruence + lead_gen_congruent + lead2_gen_congruent + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_md, weights = df_even_md$weight)
# Female R respondents 
mod3_lag_lead <- felm(approval_rep ~ lag2_gen_congruent + lag_gen_congruent + gender_congruence + lead_gen_congruent + lead2_gen_congruent + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fr, weights = df_even_fr$weight)
# Male R respondents 
mod4_lag_lead <- felm(approval_rep ~ lag2_gen_congruent + lag_gen_congruent + gender_congruence + lead_gen_congruent + lead2_gen_congruent + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_mr, weights = df_even_mr$weight)

# Basic Race 
# Nonwhite D respondents 
mod1.nw_lag_lead <- felm(approval_rep ~ lag2_race_congruent + lag_race_congruent + race_congruence + lead_race_congruent + lead2_race_congruent + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
# White D respondents 
mod2.nw_lag_lead <- felm(approval_rep ~ lag2_race_congruent + lag_race_congruent + race_congruence + lead_race_congruent + lead2_race_congruent + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)
# Nonwhite R respondents 
mod3.nw_lag_lead <- felm(approval_rep ~ lag2_race_congruent + lag_race_congruent + race_congruence + lead_race_congruent + lead2_race_congruent + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
# White R respondents 
mod4.nw_lag_lead <- felm(approval_rep ~ lag2_race_congruent + lag_race_congruent + race_congruence + lead_race_congruent + lead2_race_congruent + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wr, weights = df_even_wr$weight)

# long model names with new R version might require this fix for stargazer: https://gist.github.com/alexeyknorre/b0780836f4cec04d41a863a683f91b53
# Once using fix above, need to do library(stargazer) again 

stargazer(mod1_lag_lead, mod2_lag_lead, mod3_lag_lead, mod4_lag_lead,
          mod1.nw_lag_lead, mod2.nw_lag_lead, mod3.nw_lag_lead, mod4.nw_lag_lead,
          omit = '[i][n][d]', type='latex', header=FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels   = c("Woman MC", "POC MC"),
          column.separate = c(4, 4),
          covariate.labels=c("Gender Congruence 2-yr Lag", "Gender Congruence 1-yr Lag", "Gender Congruence = 1", 
                             "Gender Congruence 1-yr Lead", "Gender Congruence 2-yr Lead", "Racial Congruence 2-yr Lag", 
                             "Racial Congruence 1-yr Lag", "Racial Congruence = 1", "Racial Congruence 1-yr Lead", 
                             "Racial Congruence 2-yr Lead", "MC Seniority", "MC Race", "MC Gender"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X','X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X','X','X','X','X'),
                         c('Respondent Gender', 'Woman', 'Man', 'Woman', 'Man', 'All', 'All', 'All', 'All'),
                         c('Respondent Race', 'All', 'All', 'All', 'All', 'POC', 'White', 'POC', 'White'),
                         c('Respondent Party', 'D', 'D', 'R', 'R', 'D', 'D', 'R', 'R')),
          title = "Effects of MC Identity on MC Approval Ratings - Lags and Leads",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:lag-lead",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"),
          notes.append = F)

## E2: Senate check for parallel trends ----
## Basic approval for gender 
# Female D respondents 
mod2.senate <- felm(approval_sen1 ~ gender_congruence + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | 
                      state_dist, data = df_even_fd, weights = df_even_fd$weight)
# Male D respondents 
mod3.senate <- felm(approval_sen1 ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
                      state_dist, data = df_even_md, weights = df_even_md$weight)
# Female R respondents 
mod4.senate <- felm(approval_sen1 ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
                      state_dist, data = df_even_fr, weights = df_even_fr$weight)
# Male R respondents 
mod5.senate <- felm(approval_sen1 ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
                      state_dist, data = df_even_mr, weights = df_even_mr$weight)

## Basic approval for Race 
# POC D
mod2.nw.senate <- felm(approval_sen1 ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                         state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
# White D
mod3.nw.senate <- felm(approval_sen1 ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                         state_dist, data = df_even_wd, weights = df_even_wd$weight)
# POC R
mod4.nw.senate <- felm(approval_sen1 ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                         state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
# White R
mod5.nw.senate <- felm(approval_sen1 ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                         state_dist, data = df_even_wr, weights = df_even_wr$weight)

# Calculate number of treated districts in each dataset
treat2 <- df_even_fd %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod2_t <- length(which(treat2$treated==1))

treat3 <- df_even_md %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod3_t <- length(which(treat3$treated==1))

treat4 <- df_even_fr %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod4_t <- length(which(treat4$treated==1))

treat5 <- df_even_mr %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod5_t <- length(which(treat5$treated==1))

# Manually in overleaf add to other table below in panels 
stargazer(mod2.senate, mod3.senate, mod4.senate, mod5.senate,
          omit = c('[i][n][d]'), type='latex', header=FALSE, style="ajps",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c("Senate Approval"),
          column.labels = c("\\shortstack{(1) D Women \\\\ Respondents}", "\\shortstack{(2) D Men \\\\ Respondents}", "\\shortstack{(3) R Women \\\\ Respondents}", "\\shortstack{(4) R Men \\\\ Respondents}"),
          covariate.labels=c("Gender Congruence = 1", "MC Seniority", "POC MC = 1"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X'),
                         c("N districts w MC gender change", mod2_t, mod3_t, mod4_t, mod5_t)),
          title = "Effects of MC-Constituent Gender Congruence on Senate Approval Ratings",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:gender",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)

# Calculate number of treated districts in each dataset
treat2.nw <- df_even_nwd %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod2_t.nw <- length(which(treat2.nw$treated==1))

treat3.nw <- df_even_wd %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod3_t.nw <- length(which(treat3.nw$treated==1))

treat4.nw <- df_even_nwr %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod4_t.nw <- length(which(treat4.nw$treated==1))

treat5.nw <- df_even_wr %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod5_t.nw <- length(which(treat5.nw$treated==1))

stargazer(mod2.nw.senate, mod3.nw.senate, mod4.nw.senate, mod5.nw.senate,
          omit = c('[i][n][d]'), type='latex', header=FALSE, style="ajps",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{(1) D POC \\\\ Respondents}", "\\shortstack{(2) D White \\\\ Respondents}", "\\shortstack{(3) R POC \\\\ Respondents}", "\\shortstack{(4) R White \\\\ Respondents}"),
          covariate.labels=c("Racial Congruence = 1", "MC Seniority", "Woman MC = 1"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X'),
                         c("N districts w MC race change", mod2_t.nw, mod3_t.nw, mod4_t.nw, mod5_t.nw)),
          title = "Effects of MC-Constituent Racial Congruence on Senate Approval Ratings",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:POC",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)
#
## F3: Senate Placebo at all levels of ideo ----

## Basic approval for gender 
# Female D respondents 
#mod2.senate <- felm(approval_sen1 ~ gender_congruence + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | 
#                      state_dist, data = df_even_fd, weights = df_even_fd$weight)
# Male D respondents 
#mod3.senate <- felm(approval_sen1 ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
#                      state_dist, data = df_even_md, weights = df_even_md$weight)
# Female R respondents 
#mod4.senate <- felm(approval_sen1 ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
#                      state_dist, data = df_even_fr, weights = df_even_fr$weight)
# Male R respondents 
#mod5.senate <- felm(approval_sen1 ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
#                      state_dist, data = df_even_mr, weights = df_even_mr$weight)


## DW-NOMINATE ideology for gender 

#mod2_ideo.senate <- felm(approval_sen1 ~ inc_tercile_ideo1 + gender_congruence:inc_tercile_ideo1 + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fd, weights = df_even_fd$weight)

#mod3_ideo.senate <- felm(approval_sen1 ~ inc_tercile_ideo1 + gender_congruence:inc_tercile_ideo1 + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_md, weights = df_even_md$weight)

#mod4_ideo.senate <- felm(approval_sen1 ~ inc_tercile_ideo1 + gender_congruence:inc_tercile_ideo1 + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fr, weights = df_even_fr$weight)

#mod5_ideo.senate <- felm(approval_sen1 ~ inc_tercile_ideo1 + gender_congruence:inc_tercile_ideo1 + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_mr, weights = df_even_mr$weight)


## Perceived ideology for gender 

mod2_p.ideo.senate <- felm(approval_sen1 ~ inc_tercile_ideo2 + gender_congruence:inc_tercile_ideo2 + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fd, weights = df_even_fd$weight)

mod3_p.ideo.senate <- felm(approval_sen1 ~ inc_tercile_ideo2 + gender_congruence:inc_tercile_ideo2 + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_md, weights = df_even_md$weight)

mod4_p.ideo.senate <- felm(approval_sen1 ~ inc_tercile_ideo2 + gender_congruence:inc_tercile_ideo2 + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fr, weights = df_even_fr$weight)

mod5_p.ideo.senate <- felm(approval_sen1 ~ inc_tercile_ideo2 + gender_congruence:inc_tercile_ideo2 + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_mr, weights = df_even_mr$weight)

# Create data frames with regression output of interest
# Democratic women 
#mod_gen_ideo.senate <- broom::tidy(mod2_ideo.senate) %>% filter(term%in%c("inc_tercile_ideo11:gender_congruence", "inc_tercile_ideo12:gender_congruence", "inc_tercile_ideo13:gender_congruence")) %>% mutate(model = "Ideological Congruence")
mod_gen_p.ideo.senate <- broom::tidy(mod2_p.ideo.senate) %>% filter(term%in%c("inc_tercile_ideo21:gender_congruence", "inc_tercile_ideo22:gender_congruence", "inc_tercile_ideo23:gender_congruence")) %>% mutate(model = "Perceived Ideological Congruence")
# Democratic men 
#mod_gen_ideo2.senate <- broom::tidy(mod3_ideo.senate) %>% filter(term%in%c("inc_tercile_ideo11:gender_congruence", "inc_tercile_ideo12:gender_congruence", "inc_tercile_ideo13:gender_congruence")) %>% mutate(model = "Ideological Congruence")
mod_gen_p.ideo2.senate <- broom::tidy(mod3_p.ideo.senate) %>% filter(term%in%c("inc_tercile_ideo21:gender_congruence", "inc_tercile_ideo22:gender_congruence", "inc_tercile_ideo23:gender_congruence")) %>% mutate(model = "Perceived Ideological Congruence")
# Republican women
#mod_gen_ideo3.senate <- broom::tidy(mod4_ideo.senate) %>% filter(term%in%c("inc_tercile_ideo11:gender_congruence", "inc_tercile_ideo12:gender_congruence", "inc_tercile_ideo13:gender_congruence")) %>% mutate(model = "Ideological Congruence")
mod_gen_p.ideo3.senate <- broom::tidy(mod4_p.ideo.senate) %>% filter(term%in%c("inc_tercile_ideo21:gender_congruence", "inc_tercile_ideo22:gender_congruence", "inc_tercile_ideo23:gender_congruence")) %>% mutate(model = "Perceived Ideological Congruence")
# Republican men
#mod_gen_ideo4.senate <- broom::tidy(mod5_ideo.senate) %>% filter(term%in%c("inc_tercile_ideo11:gender_congruence", "inc_tercile_ideo12:gender_congruence", "inc_tercile_ideo13:gender_congruence")) %>% mutate(model = "Ideological Congruence")
mod_gen_p.ideo4.senate <- broom::tidy(mod5_p.ideo.senate) %>% filter(term%in%c("inc_tercile_ideo21:gender_congruence", "inc_tercile_ideo22:gender_congruence", "inc_tercile_ideo23:gender_congruence")) %>% mutate(model = "Perceived Ideological Congruence")

# Create tibble with all regression output for Democratic women
term <- c("Congruence", "Mid-Level Congruence", "Incongruence", "Congruence", "Mid-Level Congruence", "Incongruence")

models1.senate <- rbind(mod_gen_p.ideo.senate) %>% select(-term)
models1.senate <- cbind(models1.senate, term)
models2.senate <- rbind(mod_gen_p.ideo2.senate) %>% select(-term)
models2.senate <- cbind(models2.senate, term)
models3.senate <- rbind(mod_gen_p.ideo3.senate) %>% select(-term)
models3.senate <- cbind(models3.senate, term)
models4.senate <- rbind(mod_gen_p.ideo4.senate) %>% select(-term)
models4.senate <- cbind(models4.senate, term)

## Basic approval for Race 
#mod2.nw.senate <- felm(approval_sen1 ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
#                         state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
#mod3.nw.senate <- felm(approval_sen1 ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
#                         state_dist, data = df_even_wd, weights = df_even_wd$weight)
#mod4.nw.senate <- felm(approval_sen1 ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
#                         state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
#mod5.nw.senate <- felm(approval_sen1 ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
#                         state_dist, data = df_even_wr, weights = df_even_wr$weight)


## DW-NOMINATE ideology for nonwhite 
#mod2_ideo.nw.senate <- felm(approval_sen1 ~ inc_tercile_ideo1 + race_congruence:inc_tercile_ideo1 + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)

#mod3_ideo.nw.senate <- felm(approval_sen1 ~ inc_tercile_ideo1 + race_congruence:inc_tercile_ideo1 + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)

#mod4_ideo.nw.senate <- felm(approval_sen1 ~ inc_tercile_ideo1 + race_congruence:inc_tercile_ideo1 + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwr, weights = df_even_nwr$weight)

#mod5_ideo.nw.senate <- felm(approval_sen1 ~ inc_tercile_ideo1 + race_congruence:inc_tercile_ideo1 + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wr, weights = df_even_wr$weight)


## Perceived ideology for nonwhite 
mod2_p.ideo.nw.senate <- felm(approval_sen1 ~ inc_tercile_ideo2 + race_congruence:inc_tercile_ideo2 + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)

mod3_p.ideo.nw.senate <- felm(approval_sen1 ~ inc_tercile_ideo2 + race_congruence:inc_tercile_ideo2 + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)

mod4_p.ideo.nw.senate <- felm(approval_sen1 ~ inc_tercile_ideo2 + race_congruence:inc_tercile_ideo2 + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwr, weights = df_even_nwr$weight)

mod5_p.ideo.nw.senate <- felm(approval_sen1 ~ inc_tercile_ideo2 + race_congruence:inc_tercile_ideo2 + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wr, weights = df_even_wr$weight)

# Create data frames with regression output of interest
# Democratic nonwhite 
#mod_nw_ideo.senate <- broom::tidy(mod2_ideo.nw.senate) %>% filter(term%in%c("inc_tercile_ideo11:race_congruence", "inc_tercile_ideo12:race_congruence", "inc_tercile_ideo13:race_congruence")) %>% mutate(model = "Ideological Congruence")
mod_nw_p.ideo.senate <- broom::tidy(mod2_p.ideo.nw.senate) %>% filter(term%in%c("inc_tercile_ideo21:race_congruence", "inc_tercile_ideo22:race_congruence", "inc_tercile_ideo23:race_congruence")) %>% mutate(model = "Perceived Ideological Congruence")
# Democratic white 
#mod_nw_ideo2.senate <- broom::tidy(mod3_ideo.nw.senate) %>% filter(term%in%c("inc_tercile_ideo11:race_congruence", "inc_tercile_ideo12:race_congruence", "inc_tercile_ideo13:race_congruence")) %>% mutate(model = "Ideological Congruence")
mod_nw_p.ideo2.senate <- broom::tidy(mod3_p.ideo.nw.senate) %>% filter(term%in%c("inc_tercile_ideo21:race_congruence", "inc_tercile_ideo22:race_congruence", "inc_tercile_ideo23:race_congruence")) %>% mutate(model = "Perceived Ideological Congruence")
# Republican nonwhite
#mod_nw_ideo3.senate <- broom::tidy(mod4_ideo.nw.senate) %>% filter(term%in%c("inc_tercile_ideo11:race_congruence", "inc_tercile_ideo12:race_congruence", "inc_tercile_ideo13:race_congruence")) %>% mutate(model = "Ideological Congruence")
mod_nw_p.ideo3.senate <- broom::tidy(mod4_p.ideo.nw.senate) %>% filter(term%in%c("inc_tercile_ideo21:race_congruence", "inc_tercile_ideo22:race_congruence", "inc_tercile_ideo23:race_congruence")) %>% mutate(model = "Perceived Ideological Congruence")
# Republican white
#mod_nw_ideo4.senate <- broom::tidy(mod5_ideo.nw.senate) %>% filter(term%in%c("inc_tercile_ideo11:race_congruence", "inc_tercile_ideo12:race_congruence", "inc_tercile_ideo13:race_congruence")) %>% mutate(model = "Ideological Congruence")
mod_nw_p.ideo4.senate <- broom::tidy(mod5_p.ideo.nw.senate) %>% filter(term%in%c("inc_tercile_ideo21:race_congruence", "inc_tercile_ideo22:race_congruence", "inc_tercile_ideo23:race_congruence")) %>% mutate(model = "Perceived Ideological Congruence")

# Create tibble with all regression output for Democratic women
term <- c("Congruence", "Mid-Level Congruence", "Incongruence", "Congruence", "Mid-Level Congruence", "Incongruence")

models1.nw.senate <- rbind(mod_nw_p.ideo.senate) %>% select(-term)
models1.nw.senate <- cbind(models1.nw.senate, term)
models2.nw.senate <- rbind(mod_nw_p.ideo2.senate) %>% select(-term)
models2.nw.senate <- cbind(models2.nw.senate, term)
models3.nw.senate <- rbind(mod_nw_p.ideo3.senate) %>% select(-term)
models3.nw.senate <- cbind(models3.nw.senate, term)
models4.nw.senate <- rbind(mod_nw_p.ideo4.senate) %>% select(-term)
models4.nw.senate <- cbind(models4.nw.senate, term)

# Need to bind the datasets for face_grid 
models1.senate <- models1.senate %>% mutate(identity = "Women Respondents",
                                            model = "Democrats")
models2.senate <- models2.senate %>% mutate(identity = "Men Respondents",
                                            model = "Democrats")
models3.senate <- models3.senate %>% mutate(identity = "Women Respondents",
                                            model = "Republicans")
models4.senate <- models4.senate %>% mutate(identity = "Men Respondents",
                                            model = "Republicans")

models.all.senate <- rbind(models1.senate, models2.senate, models3.senate, models4.senate)


models1.nw.senate <- models1.nw.senate %>% mutate(identity = "POC Respondents",
                                                  model = "Democrats")
models2.nw.senate <- models2.nw.senate %>% mutate(identity = "White Respondents",
                                                  model = "Democrats")
models3.nw.senate <- models3.nw.senate %>% mutate(identity = "POC Respondents",
                                                  model = "Republicans")
models4.nw.senate <- models4.nw.senate %>% mutate(identity = "White Respondents",
                                                  model = "Republicans")

models.all.nw.senate <- rbind(models1.nw.senate, models2.nw.senate, models3.nw.senate, models4.nw.senate)


## Combining Race and Gender into one figure  
models.all.senate <- models.all.senate %>% mutate(type = "Gender Congruence")
models.all.nw.senate <- models.all.nw.senate %>% mutate(type = "Race Congruence")

both.models.senate <- rbind(models.all.senate, models.all.nw.senate)

both.models.senate$identity <- factor(both.models.senate$identity, 
                                      levels = c("Women Respondents", "Men Respondents", "POC Respondents", "White Respondents"))

both.models.senate$type <- factor(both.models.senate$type, 
                                  levels = c("Gender Congruence", "Race Congruence"))

dwplot(both.models.senate,
       vline = geom_vline(xintercept = 0,
                          colour = "grey60",
                          linetype = 2),
       dot_args = list(aes(shape = model), size=1.5),
       whisker_args = list(aes(color = model))) +
  theme_bw() + xlab("Marginal Effect of MC Identity Congruence on Senate Approval") + ylab("") +
  #ggtitle("Marginal Effect of MC Identity Congruence on Senate Approval by Type and Level of Incongruence") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 10, face = "bold", hjust = 0.5),
        axis.text.y = element_text(size = 8),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10)) + 
  scale_colour_manual(values = c("grey68", "gray35", "black")) +
  xlim(-.7, .8) +
  scale_y_discrete(limits=rev) +
  coord_flip() +
  facet_wrap(type ~ identity) +
  guides(shape = guide_legend("model"), 
         colour = guide_legend("model"))

## F. Alternative Estimates ----
# Female Democrats First ----
## 1) make 2-session subsets 
## Even white democrats - year subsets with new variable for 1 to 0 switch 
sub_0810_fd <- df_even_fd %>%
  # filter years 
  filter(cong==110 | cong==111) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1012_fd <- df_even_fd %>%
  filter(cong==111 | cong==112) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1214_fd <- df_even_fd %>%
  filter(cong==112 | cong==113) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1416_fd <- df_even_fd %>%
  filter(cong==113 | cong==114) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1618_fd <- df_even_fd %>%
  filter(cong==114 | cong==115) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1820_fd <- df_even_fd %>%
  filter(cong==115 | cong==116) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))

## 2) Remove any switches from 1 to 0 
# Calculate the number of 1 to 0 switches in each subset 
sub_0810_switch_fd <- sub_0810_fd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1012_switch_fd <- sub_1012_fd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1214_switch_fd <- sub_1214_fd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1416_switch_fd <- sub_1416_fd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1618_switch_fd <- sub_1618_fd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1820_switch_fd <- sub_1820_fd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

# Remove the rows where bad_switch is 1 (anywhere that became 0 after 1) - we only want the 0 to 1 switches 
sub_0810_fd.2 <- sub_0810_fd %>%
  filter(bad_switch == 0)
sub_1012_fd.2 <- sub_1012_fd %>%
  filter(bad_switch == 0)
sub_1214_fd.2 <- sub_1214_fd %>%
  filter(bad_switch == 0)
sub_1416_fd.2 <- sub_1416_fd %>%
  filter(bad_switch == 0)
sub_1618_fd.2 <- sub_1618_fd %>%
  filter(bad_switch == 0)
sub_1820_fd.2 <- sub_1820_fd %>%
  filter(bad_switch == 0)

# Female Republicans Second ----
## 1) make 2-session subsets 
## year subsets with new variable for 1 to 0 switch 
sub_0810_fr <- df_even_fr %>%
  # filter years 
  filter(cong==110 | cong==111) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1012_fr <- df_even_fr %>%
  filter(cong==111 | cong==112) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1214_fr <- df_even_fr %>%
  filter(cong==112 | cong==113) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1416_fr <- df_even_fr %>%
  filter(cong==113 | cong==114) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1618_fr <- df_even_fr %>%
  filter(cong==114 | cong==115) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1820_fr <- df_even_fr %>%
  filter(cong==115 | cong==116) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))

# Calculate the number of 1 to 0 switches in each subset 
sub_0810_switch_fr <- sub_0810_fr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1012_switch_fr <- sub_1012_fr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1214_switch_fr <- sub_1214_fr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1416_switch_fr <- sub_1416_fr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1618_switch_fr <- sub_1618_fr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1820_switch_fr <- sub_1820_fr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

# Remove the rows where bad_switch is 1 (anywhere that became 0 after 1) - we only want the 0 to 1 switches 
sub_0810_fr.2 <- sub_0810_fr %>%
  filter(bad_switch == 0)
sub_1012_fr.2 <- sub_1012_fr %>%
  filter(bad_switch == 0)
sub_1214_fr.2 <- sub_1214_fr %>%
  filter(bad_switch == 0)
sub_1416_fr.2 <- sub_1416_fr %>%
  filter(bad_switch == 0)
sub_1618_fr.2 <- sub_1618_fr %>%
  filter(bad_switch == 0)
sub_1820_fr.2 <- sub_1820_fr %>%
  filter(bad_switch == 0)

# Male Democrats Third ----
## 1) make 2-session subsets 
## year subsets with new variable for 1 to 0 switch 
sub_0810_md <- df_even_md %>%
  # filter years 
  filter(cong==110 | cong==111) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1012_md <- df_even_md %>%
  filter(cong==111 | cong==112) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1214_md <- df_even_md %>%
  filter(cong==112 | cong==113) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1416_md <- df_even_md %>%
  filter(cong==113 | cong==114) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1618_md <- df_even_md %>%
  filter(cong==114 | cong==115) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1820_md <- df_even_md %>%
  filter(cong==115 | cong==116) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))

## 2) Remove any switches from 1 to 0 
# Calculate the number of 1 to 0 switches in each subset 
sub_0810_switch_md <- sub_0810_md %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1012_switch_md <- sub_1012_md %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1214_switch_md <- sub_1214_md %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1416_switch_md <- sub_1416_md %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1618_switch_md <- sub_1618_md %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1820_switch_md <- sub_1820_md %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

# Remove the rows where bad_switch is 1 (anywhere that became 0 after 1) - we only want the 0 to 1 switches 
sub_0810_md.2 <- sub_0810_md %>%
  filter(bad_switch == 0)
sub_1012_md.2 <- sub_1012_md %>%
  filter(bad_switch == 0)
sub_1214_md.2 <- sub_1214_md %>%
  filter(bad_switch == 0)
sub_1416_md.2 <- sub_1416_md %>%
  filter(bad_switch == 0)
sub_1618_md.2 <- sub_1618_md %>%
  filter(bad_switch == 0)
sub_1820_md.2 <- sub_1820_md %>%
  filter(bad_switch == 0)

# Male Republicans Fourth ----
## 1) make 2-session subsets 
## year subsets with new variable for 1 to 0 switch 
sub_0810_mr <- df_even_mr %>%
  # filter years 
  filter(cong==110 | cong==111) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1012_mr <- df_even_mr %>%
  filter(cong==111 | cong==112) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1214_mr <- df_even_mr %>%
  filter(cong==112 | cong==113) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1416_mr <- df_even_mr %>%
  filter(cong==113 | cong==114) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1618_mr <- df_even_mr %>%
  filter(cong==114 | cong==115) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))
sub_1820_mr <- df_even_mr %>%
  filter(cong==115 | cong==116) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(mc_gender==0 & lead_gender_mc==1 ~ 1, TRUE ~ 0))

# Calculate the number of 1 to 0 switches in each subset 
sub_0810_switch_mr <- sub_0810_mr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1012_switch_mr <- sub_1012_mr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1214_switch_mr <- sub_1214_mr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1416_switch_mr <- sub_1416_mr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1618_switch_mr <- sub_1618_mr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1820_switch_mr <- sub_1820_mr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

# Remove the rows where bad_switch is 1 (anywhere that became 0 after 1) - we only want the 0 to 1 switches 
sub_0810_mr.2 <- sub_0810_mr %>%
  filter(bad_switch == 0)
sub_1012_mr.2 <- sub_1012_mr %>%
  filter(bad_switch == 0)
sub_1214_mr.2 <- sub_1214_mr %>%
  filter(bad_switch == 0)
sub_1416_mr.2 <- sub_1416_mr %>%
  filter(bad_switch == 0)
sub_1618_mr.2 <- sub_1618_mr %>%
  filter(bad_switch == 0)
sub_1820_mr.2 <- sub_1820_mr %>%
  filter(bad_switch == 0)


# 3) Average weighted by # of treated for each pair 
# Calculate the number of 0 to 1 switches to get weights 
# Female Democrats 
sub_0810_temp <- sub_0810_fd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated <- sub_0810_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight1_fd <- nrow(treated) 

sub_1012_temp <- sub_1012_fd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated2 <- sub_1012_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight2_fd <- nrow(treated2) 

sub_1214_temp <- sub_1214_fd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated3 <- sub_1214_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight3_fd <- nrow(treated3) 

sub_1416_temp <- sub_1416_fd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated4 <- sub_1416_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight4_fd <- nrow(treated4) 

sub_1618_temp <- sub_1618_fd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated5 <- sub_1618_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight5_fd <- nrow(treated5) 

sub_1820_temp <- sub_1820_fd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated6 <- sub_1820_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight6_fd <- nrow(treated6) 

# Female Republicans 
sub_0810_temp <- sub_0810_fr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated <- sub_0810_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight1_fr <- nrow(treated) 

sub_1012_temp <- sub_1012_fr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated2 <- sub_1012_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight2_fr <- nrow(treated2) 

sub_1214_temp <- sub_1214_fr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated3 <- sub_1214_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight3_fr <- nrow(treated3) 

sub_1416_temp <- sub_1416_fr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated4 <- sub_1416_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight4_fr <- nrow(treated4) 

sub_1618_temp <- sub_1618_fr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated5 <- sub_1618_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight5_fr <- nrow(treated5) 

sub_1820_temp <- sub_1820_fr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated6 <- sub_1820_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight6_fr <- nrow(treated6) 

# Male Democrats 
sub_0810_temp <- sub_0810_md.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated <- sub_0810_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight1_md <- nrow(treated) 

sub_1012_temp <- sub_1012_md.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated2 <- sub_1012_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight2_md <- nrow(treated2) 

sub_1214_temp <- sub_1214_md.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated3 <- sub_1214_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight3_md <- nrow(treated3) 

sub_1416_temp <- sub_1416_md.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated4 <- sub_1416_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight4_md <- nrow(treated4) 

sub_1618_temp <- sub_1618_md.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated5 <- sub_1618_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight5_md <- nrow(treated5) 

sub_1820_temp <- sub_1820_md.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated6 <- sub_1820_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight6_md <- nrow(treated6) 

# Male Republicans 
sub_0810_temp <- sub_0810_mr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated <- sub_0810_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight1_mr <- nrow(treated) 

sub_1012_temp <- sub_1012_mr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated2 <- sub_1012_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight2_mr <- nrow(treated2) 

sub_1214_temp <- sub_1214_mr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated3 <- sub_1214_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight3_mr <- nrow(treated3) 

sub_1416_temp <- sub_1416_mr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated4 <- sub_1416_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight4_mr <- nrow(treated4) 

sub_1618_temp <- sub_1618_mr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated5 <- sub_1618_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight5_mr <- nrow(treated5) 

sub_1820_temp <- sub_1820_mr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  group_by(state_dist) %>%
  mutate(pct_mc_gender = as.numeric(mc_gender)) %>%
  summarise_at(vars(pct_mc_gender), mean, na.rm=T)
treated6 <- sub_1820_temp %>% filter(pct_mc_gender > 0 & pct_mc_gender < 1)
weight6_mr <- nrow(treated6) 

# POC Democrats First ----
## 1) make 2-session subsets 
## Year subsets with new variable for 1 to 0 switch 
sub_0810_nwd <- df_even_nwd %>%
  # filter years 
  filter(cong==110 | cong==111) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1012_nwd <- df_even_nwd %>%
  filter(cong==111 | cong==112) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1214_nwd <- df_even_nwd %>%
  filter(cong==112 | cong==113) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1416_nwd <- df_even_nwd %>%
  filter(cong==113 | cong==114) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1618_nwd <- df_even_nwd %>%
  filter(cong==114 | cong==115) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1820_nwd <- df_even_nwd %>%
  filter(cong==115 | cong==116) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))

## 2) Remove any switches from 1 to 0 
# Calculate the number of 1 to 0 switches in each subset 
sub_0810_switch_nwd <- sub_0810_nwd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1012_switch_nwd <- sub_1012_nwd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1214_switch_nwd <- sub_1214_nwd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1416_switch_nwd <- sub_1416_nwd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1618_switch_nwd <- sub_1618_nwd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1820_switch_nwd <- sub_1820_nwd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

# Remove the rows where bad_switch is 1 (anywhere that became 0 after 1) - we only want the 0 to 1 switches 
sub_0810_nwd.2 <- sub_0810_nwd %>%
  filter(bad_switch == 0)
sub_1012_nwd.2 <- sub_1012_nwd %>%
  filter(bad_switch == 0)
sub_1214_nwd.2 <- sub_1214_nwd %>%
  filter(bad_switch == 0)
sub_1416_nwd.2 <- sub_1416_nwd %>%
  filter(bad_switch == 0)
sub_1618_nwd.2 <- sub_1618_nwd %>%
  filter(bad_switch == 0)
sub_1820_nwd.2 <- sub_1820_nwd %>%
  filter(bad_switch == 0)

# POC Republicans Second ----
## 1) make 2-session subsets 
## year subsets with new variable for 1 to 0 switch 
sub_0810_nwr <- df_even_nwr %>%
  # filter years 
  filter(cong==110 | cong==111) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1012_nwr <- df_even_nwr %>%
  filter(cong==111 | cong==112) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1214_nwr <- df_even_nwr %>%
  filter(cong==112 | cong==113) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1416_nwr <- df_even_nwr %>%
  filter(cong==113 | cong==114) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1618_nwr <- df_even_nwr %>%
  filter(cong==114 | cong==115) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1820_nwr <- df_even_nwr %>%
  filter(cong==115 | cong==116) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))

# Calculate the number of 1 to 0 switches in each subset 
sub_0810_switch_nwr <- sub_0810_nwr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1012_switch_nwr <- sub_1012_nwr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1214_switch_nwr <- sub_1214_nwr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1416_switch_nwr <- sub_1416_nwr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1618_switch_nwr <- sub_1618_nwr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1820_switch_nwr <- sub_1820_nwr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

# Remove the rows where bad_switch is 1 (anywhere that became 0 after 1) - we only want the 0 to 1 switches 
sub_0810_nwr.2 <- sub_0810_nwr %>%
  filter(bad_switch == 0)
sub_1012_nwr.2 <- sub_1012_nwr %>%
  filter(bad_switch == 0)
sub_1214_nwr.2 <- sub_1214_nwr %>%
  filter(bad_switch == 0)
sub_1416_nwr.2 <- sub_1416_nwr %>%
  filter(bad_switch == 0)
sub_1618_nwr.2 <- sub_1618_nwr %>%
  filter(bad_switch == 0)
sub_1820_nwr.2 <- sub_1820_nwr %>%
  filter(bad_switch == 0)

# White Democrats Third ----
## 1) make 2-session subsets 
## year subsets with new variable for 1 to 0 switch 
sub_0810_wd <- df_even_wd %>%
  # filter years 
  filter(cong==110 | cong==111) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1012_wd <- df_even_wd %>%
  filter(cong==111 | cong==112) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1214_wd <- df_even_wd %>%
  filter(cong==112 | cong==113) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1416_wd <- df_even_wd %>%
  filter(cong==113 | cong==114) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1618_wd <- df_even_wd %>%
  filter(cong==114 | cong==115) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1820_wd <- df_even_wd %>%
  filter(cong==115 | cong==116) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))

## 2) Remove any switches from 1 to 0 
# Calculate the number of 1 to 0 switches in each subset 
sub_0810_switch_wd <- sub_0810_wd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1012_switch_wd <- sub_1012_wd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1214_switch_wd <- sub_1214_wd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1416_switch_wd <- sub_1416_wd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1618_switch_wd <- sub_1618_wd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1820_switch_wd <- sub_1820_wd %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

# Remove the rows where bad_switch is 1 (anywhere that became 0 after 1) - we only want the 0 to 1 switches 
sub_0810_wd.2 <- sub_0810_wd %>%
  filter(bad_switch == 0)
sub_1012_wd.2 <- sub_1012_wd %>%
  filter(bad_switch == 0)
sub_1214_wd.2 <- sub_1214_wd %>%
  filter(bad_switch == 0)
sub_1416_wd.2 <- sub_1416_wd %>%
  filter(bad_switch == 0)
sub_1618_wd.2 <- sub_1618_wd %>%
  filter(bad_switch == 0)
sub_1820_wd.2 <- sub_1820_wd %>%
  filter(bad_switch == 0)

# White Republicans Fourth ----
## 1) make 2-session subsets 
## year subsets with new variable for 1 to 0 switch 
sub_0810_wr <- df_even_wr %>%
  # filter years 
  filter(cong==110 | cong==111) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1012_wr <- df_even_wr %>%
  filter(cong==111 | cong==112) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1214_wr <- df_even_wr %>%
  filter(cong==112 | cong==113) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1416_wr <- df_even_wr %>%
  filter(cong==113 | cong==114) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1618_wr <- df_even_wr %>%
  filter(cong==114 | cong==115) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))
sub_1820_wr <- df_even_wr %>%
  filter(cong==115 | cong==116) %>%
  # generate switch variable 
  mutate(bad_switch = case_when(nonwhite_mc==0 & lead_nonwhite_mc==1 ~ 1, TRUE ~ 0))

# Calculate the number of 1 to 0 switches in each subset 
sub_0810_switch_wr <- sub_0810_wr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1012_switch_wr <- sub_1012_wr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1214_switch_wr <- sub_1214_wr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1416_switch_wr <- sub_1416_wr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1618_switch_wr <- sub_1618_wr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

sub_1820_switch_wr <- sub_1820_wr %>%
  group_by(state_dist, cong) %>%
  summarise_at(vars(bad_switch), sum, na.rm=T) %>%
  filter(bad_switch>0)

# Remove the rows where bad_switch is 1 (anywhere that became 0 after 1) - we only want the 0 to 1 switches 
sub_0810_wr.2 <- sub_0810_wr %>%
  filter(bad_switch == 0)
sub_1012_wr.2 <- sub_1012_wr %>%
  filter(bad_switch == 0)
sub_1214_wr.2 <- sub_1214_wr %>%
  filter(bad_switch == 0)
sub_1416_wr.2 <- sub_1416_wr %>%
  filter(bad_switch == 0)
sub_1618_wr.2 <- sub_1618_wr %>%
  filter(bad_switch == 0)
sub_1820_wr.2 <- sub_1820_wr %>%
  filter(bad_switch == 0)


## 3) Average weighted by # of treated for each pair
# Calculate the number of 0 to 1 switches to get weights 
# POC Democrats 
sub_0810_temp <- sub_0810_nwd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated <- sub_0810_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight1_nwd <- nrow(treated) 

sub_1012_temp <- sub_1012_nwd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated2 <- sub_1012_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight2_nwd <- nrow(treated2) 

sub_1214_temp <- sub_1214_nwd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated3 <- sub_1214_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight3_nwd <- nrow(treated3) 

sub_1416_temp <- sub_1416_nwd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated4 <- sub_1416_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight4_nwd <- nrow(treated4) 

sub_1618_temp <- sub_1618_nwd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated5 <- sub_1618_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight5_nwd <- nrow(treated5) 

sub_1820_temp <- sub_1820_nwd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated6 <- sub_1820_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight6_nwd <- nrow(treated6) 

# POC Republicans 
sub_0810_temp <- sub_0810_nwr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated <- sub_0810_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight1_nwr <- nrow(treated) 

sub_1012_temp <- sub_1012_nwr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated2 <- sub_1012_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight2_nwr <- nrow(treated2) 

sub_1214_temp <- sub_1214_nwr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated3 <- sub_1214_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight3_nwr <- nrow(treated3) 

sub_1416_temp <- sub_1416_nwr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated4 <- sub_1416_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight4_nwr <- nrow(treated4) 

sub_1618_temp <- sub_1618_nwr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated5 <- sub_1618_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight5_nwr <- nrow(treated5) 

sub_1820_temp <- sub_1820_nwr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated6 <- sub_1820_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight6_nwr <- nrow(treated6) 

# White Democrats 
sub_0810_temp <- sub_0810_wd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated <- sub_0810_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight1_wd <- nrow(treated) 

sub_1012_temp <- sub_1012_wd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated2 <- sub_1012_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight2_wd <- nrow(treated2) 

sub_1214_temp <- sub_1214_wd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated3 <- sub_1214_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight3_wd <- nrow(treated3) 

sub_1416_temp <- sub_1416_wd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated4 <- sub_1416_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight4_wd <- nrow(treated4) 

sub_1618_temp <- sub_1618_wd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated5 <- sub_1618_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight5_wd <- nrow(treated5) 

sub_1820_temp <- sub_1820_wd.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated6 <- sub_1820_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight6_wd <- nrow(treated6) 

# White Republicans 
sub_0810_temp <- sub_0810_wr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated <- sub_0810_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight1_wr <- nrow(treated) 

sub_1012_temp <- sub_1012_wr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated2 <- sub_1012_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight2_wr <- nrow(treated2) 

sub_1214_temp <- sub_1214_wr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated3 <- sub_1214_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight3_wr <- nrow(treated3) 

sub_1416_temp <- sub_1416_wr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated4 <- sub_1416_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight4_wr <- nrow(treated4) 

sub_1618_temp <- sub_1618_wr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated5 <- sub_1618_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight5_wr <- nrow(treated5) 

sub_1820_temp <- sub_1820_wr.2 %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  group_by(state_dist) %>%
  mutate(pct_nonwhite_mc = as.numeric(nonwhite_mc)) %>%
  summarise_at(vars(pct_nonwhite_mc), mean, na.rm=T)
treated6 <- sub_1820_temp %>% filter(pct_nonwhite_mc > 0 & pct_nonwhite_mc < 1)
weight6_wr <- nrow(treated6) 

# Democratic Respondents Gender TABLE ----

## Bivariate Approval on Woman MC with controls 
# Create Function to run regression with different datasets and retrieve coefficient 
bivariate <- function(data, var1, var2){
  model <- felm(approval_rep ~ var1 + mc_seniority + var2 | cong + dist_party_mc | 0 | 
                  state_dist, data=data, weights=data$weight)
  mod.coeff <- model$coefficient[[1]]
  mod.coeff
}

full_basic_fd <- bivariate(df_even_fd, df_even_fd$gender_congruence, df_even_fd$nonwhite_mc)
basic_0810_fd <- bivariate(sub_0810_fd.2, sub_0810_fd.2$gender_congruence, sub_0810_fd.2$nonwhite_mc)
basic_1012_fd <- bivariate(sub_1012_fd.2, sub_1012_fd.2$gender_congruence, sub_1012_fd.2$nonwhite_mc)
basic_1214_fd <- bivariate(sub_1214_fd.2, sub_1214_fd.2$gender_congruence, sub_1214_fd.2$nonwhite_mc)
basic_1416_fd <- bivariate(sub_1416_fd.2, sub_1416_fd.2$gender_congruence, sub_1416_fd.2$nonwhite_mc)
basic_1618_fd <- bivariate(sub_1618_fd.2, sub_1618_fd.2$gender_congruence, sub_1618_fd.2$nonwhite_mc)
basic_1820_fd <- bivariate(sub_1820_fd.2, sub_1820_fd.2$gender_congruence, sub_1820_fd.2$nonwhite_mc)

full_basic_md <- bivariate(df_even_md, df_even_md$gender_congruence, df_even_md$nonwhite_mc)
basic_0810_md <- bivariate(sub_0810_md.2, sub_0810_md.2$gender_congruence, sub_0810_md.2$nonwhite_mc)
basic_1012_md <- bivariate(sub_1012_md.2, sub_1012_md.2$gender_congruence, sub_1012_md.2$nonwhite_mc)
basic_1214_md <- bivariate(sub_1214_md.2, sub_1214_md.2$gender_congruence, sub_1214_md.2$nonwhite_mc)
basic_1416_md <- bivariate(sub_1416_md.2, sub_1416_md.2$gender_congruence, sub_1416_md.2$nonwhite_mc)
basic_1618_md <- bivariate(sub_1618_md.2, sub_1618_md.2$gender_congruence, sub_1618_md.2$nonwhite_mc)
basic_1820_md <- bivariate(sub_1820_md.2, sub_1820_md.2$gender_congruence, sub_1820_md.2$nonwhite_mc)

# Generate average effect with treatment weights
# Women
effect_fd <- weighted.mean(c(basic_0810_fd,basic_1012_fd,basic_1214_fd, basic_1416_fd,basic_1618_fd,basic_1820_fd),
                           c(weight1_fd, weight2_fd, weight3_fd, weight4_fd, weight5_fd, weight6_fd))
# Men
effect_md <- weighted.mean(c(basic_0810_md,basic_1012_md,basic_1214_md, basic_1416_md,basic_1618_md,basic_1820_md),
                           c(weight1_md, weight2_md, weight3_md, weight4_md, weight5_md, weight6_md))

## + ideological incongruence
fcn.ideo <- function(data, var1, var2){
  model <- felm(approval_rep ~ var1 + inc_tercile_ideo2:var1 + mc_seniority + var2 | cong + dist_party_mc | 0 | state_dist, data = data, weights = data$weight)
  mod.ideo1 <- model$coefficient[[4]]
  mod.ideo2 <- model$coefficient[[5]]
  mod.ideo3 <- model$coefficient[[6]]
  mod.ideo4 <- model$coefficient[[7]]
  c(mod.ideo1, mod.ideo2, mod.ideo3, mod.ideo4)
}

full_ideo_fd <- fcn.ideo(df_even_fd, df_even_fd$gender_congruence.fac, df_even_fd$nonwhite_mc)
ideo_0810_fd <- fcn.ideo(sub_0810_fd.2, sub_0810_fd.2$gender_congruence.fac, sub_0810_fd.2$nonwhite_mc)
ideo_1012_fd <- fcn.ideo(sub_1012_fd.2, sub_1012_fd.2$gender_congruence.fac, sub_1012_fd.2$nonwhite_mc)
ideo_1214_fd <- fcn.ideo(sub_1214_fd.2, sub_1214_fd.2$gender_congruence.fac, sub_1214_fd.2$nonwhite_mc)
ideo_1416_fd <- fcn.ideo(sub_1416_fd.2, sub_1416_fd.2$gender_congruence.fac, sub_1416_fd.2$nonwhite_mc)
ideo_1618_fd <- fcn.ideo(sub_1618_fd.2, sub_1618_fd.2$gender_congruence.fac, sub_1618_fd.2$nonwhite_mc)
ideo_1820_fd <- fcn.ideo(sub_1820_fd.2, sub_1820_fd.2$gender_congruence.fac, sub_1820_fd.2$nonwhite_mc)

full_ideo_md <- fcn.ideo(df_even_md, df_even_md$gender_congruence.fac, df_even_md$nonwhite_mc)
ideo_0810_md <- fcn.ideo(sub_0810_md.2, sub_0810_md.2$gender_congruence.fac, sub_0810_md.2$nonwhite_mc)
ideo_1012_md <- fcn.ideo(sub_1012_md.2, sub_1012_md.2$gender_congruence.fac, sub_1012_md.2$nonwhite_mc)
ideo_1214_md <- fcn.ideo(sub_1214_md.2, sub_1214_md.2$gender_congruence.fac, sub_1214_md.2$nonwhite_mc)
ideo_1416_md <- fcn.ideo(sub_1416_md.2, sub_1416_md.2$gender_congruence.fac, sub_1416_md.2$nonwhite_mc)
ideo_1618_md <- fcn.ideo(sub_1618_md.2, sub_1618_md.2$gender_congruence.fac, sub_1618_md.2$nonwhite_mc)
ideo_1820_md <- fcn.ideo(sub_1820_md.2, sub_1820_md.2$gender_congruence.fac, sub_1820_md.2$nonwhite_mc)

# Generate average effect with treatment weights 
# Women
# tercile 1-2, gender inc 
effect_ideo1_fd <- weighted.mean(c(ideo_0810_fd[[1]],ideo_1012_fd[[1]],ideo_1214_fd[[1]], ideo_1416_fd[[1]], ideo_1618_fd[[1]], 
                                   ideo_1820_fd[[1]]), c(weight1_fd, weight2_fd, weight3_fd, weight4_fd, weight5_fd, weight6_fd))
# tercile 1-3, gender inc 
effect_ideo2_fd <- weighted.mean(c(ideo_0810_fd[[2]],ideo_1012_fd[[2]],ideo_1214_fd[[2]], ideo_1416_fd[[2]], ideo_1618_fd[[2]], 
                                   ideo_1820_fd[[2]]), c(weight1_fd, weight2_fd, weight3_fd, weight4_fd, weight5_fd, weight6_fd))
# tercile 1-2, gender cong 
effect_ideo3_fd <- weighted.mean(c(ideo_0810_fd[[3]],ideo_1012_fd[[3]],ideo_1214_fd[[3]], ideo_1416_fd[[3]], ideo_1618_fd[[3]], 
                                   ideo_1820_fd[[3]]), c(weight1_fd, weight2_fd, weight3_fd, weight4_fd, weight5_fd, weight6_fd))
# tercile 1-3, gender cong 
effect_ideo4_fd <- weighted.mean(c(ideo_0810_fd[[4]],ideo_1012_fd[[4]],ideo_1214_fd[[4]], ideo_1416_fd[[4]], ideo_1618_fd[[4]], 
                                   ideo_1820_fd[[4]]), c(weight1_fd, weight2_fd, weight3_fd, weight4_fd, weight5_fd, weight6_fd))
# Men
# tercile 1-2, gender inc 
effect_ideo1_md <- weighted.mean(c(ideo_0810_md[[1]],ideo_1012_md[[1]],ideo_1214_md[[1]], ideo_1416_md[[1]], ideo_1618_md[[1]], 
                                   ideo_1820_md[[1]]), c(weight1_md, weight2_md, weight3_md, weight4_md, weight5_md, weight6_md))
# tercile 1-3, gender inc 
effect_ideo2_md <- weighted.mean(c(ideo_0810_md[[2]],ideo_1012_md[[2]],ideo_1214_md[[2]], ideo_1416_md[[2]], ideo_1618_md[[2]], 
                                   ideo_1820_md[[2]]), c(weight1_md, weight2_md, weight3_md, weight4_md, weight5_md, weight6_md))
# tercile 1-2, gender cong 
effect_ideo3_md <- weighted.mean(c(ideo_0810_md[[3]],ideo_1012_md[[3]],ideo_1214_md[[3]], ideo_1416_md[[3]], ideo_1618_md[[3]], 
                                   ideo_1820_md[[3]]), c(weight1_md, weight2_md, weight3_md, weight4_md, weight5_md, weight6_md))
# tercile 1-3, gender cong 
effect_ideo4_md <- weighted.mean(c(ideo_0810_md[[4]],ideo_1012_md[[4]],ideo_1214_md[[4]], ideo_1416_md[[4]], ideo_1618_md[[4]], 
                                   ideo_1820_md[[4]]), c(weight1_md, weight2_md, weight3_md, weight4_md, weight5_md, weight6_md))

# Column of Basic estimate comparison women
basic_row_fd <- cbind(effect_fd, full_basic_fd)
ideo_row1_fd <- cbind(effect_ideo1_fd, full_ideo_fd[[1]])
ideo_row2_fd <- cbind(effect_ideo2_fd, full_ideo_fd[[2]])
ideo_row3_fd <- cbind(effect_ideo3_fd, full_ideo_fd[[3]])
ideo_row4_fd <- cbind(effect_ideo4_fd, full_ideo_fd[[4]])
# bind the rows 
all_effects_fd <- rbind(basic_row_fd, ideo_row1_fd, ideo_row2_fd, ideo_row3_fd,ideo_row4_fd)

# Column of Basic estimate comparison men
basic_row_md <- cbind(effect_md, full_basic_md)
ideo_row1_md <- cbind(effect_ideo1_md, full_ideo_md[[1]])
ideo_row2_md <- cbind(effect_ideo2_md, full_ideo_md[[2]])
ideo_row3_md <- cbind(effect_ideo3_md, full_ideo_md[[3]])
ideo_row4_md <- cbind(effect_ideo4_md, full_ideo_md[[4]])

# bind the rows 
all_effects_md <- rbind(basic_row_md, ideo_row1_md, ideo_row2_md, ideo_row3_md, ideo_row4_md)

# combine men and women 
all_effects_gender.d <- cbind(all_effects_fd, all_effects_md)

colnames(all_effects_gender.d) <- c("Wtd. Avg.\n Wom.", "Main Model\n Wom.", "Wtd. Avg.\n Men", "Main Model\n Men")
rownames(all_effects_gender.d) <- c("Bivariate + Controls", "Ideology Tercile 1-2 | Gender Incongruence", 
                                    "Ideology Tercile 1-3 | Gender Incongruence", 
                                    "Ideology Tercile 1-2 | Gender Congruence",
                                    "Ideology Tercile 1-3 | Gender Congruence")

stargazer(all_effects_gender.d, type="latex", header = FALSE, title = "Alternative model comparison for Gender Congruence (Democrats)", style = "apsr")

# Republican Respondents Gender TABLE ----

## Bivariate Approval on POC MC with controls 
# Create Function to run regression with different datasets and retrieve coefficient 
full_basic_fr <- bivariate(df_even_fr, df_even_fr$gender_congruence, df_even_fr$nonwhite_mc)
basic_0810_fr <- bivariate(sub_0810_fr.2, sub_0810_fr.2$gender_congruence, sub_0810_fr.2$nonwhite_mc)
basic_1012_fr <- bivariate(sub_1012_fr.2, sub_1012_fr.2$gender_congruence, sub_1012_fr.2$nonwhite_mc)
basic_1214_fr <- bivariate(sub_1214_fr.2, sub_1214_fr.2$gender_congruence, sub_1214_fr.2$nonwhite_mc)
basic_1416_fr <- bivariate(sub_1416_fr.2, sub_1416_fr.2$gender_congruence, sub_1416_fr.2$nonwhite_mc)
basic_1618_fr <- bivariate(sub_1618_fr.2, sub_1618_fr.2$gender_congruence, sub_1618_fr.2$nonwhite_mc)
basic_1820_fr <- bivariate(sub_1820_fr.2, sub_1820_fr.2$gender_congruence, sub_1820_fr.2$nonwhite_mc)

full_basic_mr <- bivariate(df_even_mr, df_even_mr$gender_congruence, df_even_mr$nonwhite_mc)
basic_0810_mr <- bivariate(sub_0810_mr.2, sub_0810_mr.2$gender_congruence, sub_0810_mr.2$nonwhite_mc)
basic_1012_mr <- bivariate(sub_1012_mr.2, sub_1012_mr.2$gender_congruence, sub_1012_mr.2$nonwhite_mc)
basic_1214_mr <- bivariate(sub_1214_mr.2, sub_1214_mr.2$gender_congruence, sub_1214_mr.2$nonwhite_mc)
basic_1416_mr <- bivariate(sub_1416_mr.2, sub_1416_mr.2$gender_congruence, sub_1416_mr.2$nonwhite_mc)
basic_1618_mr <- bivariate(sub_1618_mr.2, sub_1618_mr.2$gender_congruence, sub_1618_mr.2$nonwhite_mc)
basic_1820_mr <- bivariate(sub_1820_mr.2, sub_1820_mr.2$gender_congruence, sub_1820_mr.2$nonwhite_mc)

# Generate average effect with treatment weights
# Women
effect_fr <- weighted.mean(c(basic_0810_fr,basic_1012_fr,basic_1214_fr, basic_1416_fr,basic_1618_fr,basic_1820_fr),
                           c(weight1_fr, weight2_fr, weight3_fr, weight4_fr, weight5_fr, weight6_fr))
# Men
effect_mr <- weighted.mean(c(basic_0810_mr,basic_1012_mr,basic_1214_mr, basic_1416_mr,basic_1618_mr,basic_1820_mr),
                           c(weight1_mr, weight2_mr, weight3_mr, weight4_mr, weight5_mr, weight6_mr))

# Ideo 
full_ideo_fr <- fcn.ideo(df_even_fr, df_even_fr$gender_congruence.fac, df_even_fr$nonwhite_mc)
ideo_0810_fr <- fcn.ideo(sub_0810_fr.2, sub_0810_fr.2$gender_congruence.fac, sub_0810_fr.2$nonwhite_mc)
ideo_1012_fr <- fcn.ideo(sub_1012_fr.2, sub_1012_fr.2$gender_congruence.fac, sub_1012_fr.2$nonwhite_mc)
ideo_1214_fr <- fcn.ideo(sub_1214_fr.2, sub_1214_fr.2$gender_congruence.fac, sub_1214_fr.2$nonwhite_mc)
ideo_1416_fr <- fcn.ideo(sub_1416_fr.2, sub_1416_fr.2$gender_congruence.fac, sub_1416_fr.2$nonwhite_mc)
ideo_1618_fr <- fcn.ideo(sub_1618_fr.2, sub_1618_fr.2$gender_congruence.fac, sub_1618_fr.2$nonwhite_mc)
ideo_1820_fr <- fcn.ideo(sub_1820_fr.2, sub_1820_fr.2$gender_congruence.fac, sub_1820_fr.2$nonwhite_mc)

full_ideo_mr <- fcn.ideo(df_even_mr, df_even_mr$gender_congruence.fac, df_even_mr$nonwhite_mc)
ideo_0810_mr <- fcn.ideo(sub_0810_mr.2, sub_0810_mr.2$gender_congruence.fac, sub_0810_mr.2$nonwhite_mc)
ideo_1012_mr <- fcn.ideo(sub_1012_mr.2, sub_1012_mr.2$gender_congruence.fac, sub_1012_mr.2$nonwhite_mc)
ideo_1214_mr <- fcn.ideo(sub_1214_mr.2, sub_1214_mr.2$gender_congruence.fac, sub_1214_mr.2$nonwhite_mc)
ideo_1416_mr <- fcn.ideo(sub_1416_mr.2, sub_1416_mr.2$gender_congruence.fac, sub_1416_mr.2$nonwhite_mc)
ideo_1618_mr <- fcn.ideo(sub_1618_mr.2, sub_1618_mr.2$gender_congruence.fac, sub_1618_mr.2$nonwhite_mc)
ideo_1820_mr <- fcn.ideo(sub_1820_mr.2, sub_1820_mr.2$gender_congruence.fac, sub_1820_mr.2$nonwhite_mc)

# Generate average effect with treatment weights 
# Women
# tercile 1-2, gender inc 
effect_ideo1_fr <- weighted.mean(c(ideo_0810_fr[[1]],ideo_1012_fr[[1]],ideo_1214_fr[[1]], ideo_1416_fr[[1]], ideo_1618_fr[[1]], 
                                   ideo_1820_fr[[1]]), c(weight1_fr, weight2_fr, weight3_fr, weight4_fr, weight5_fr, weight6_fr))
# tercile 1-3, gender inc 
effect_ideo2_fr <- weighted.mean(c(ideo_0810_fr[[2]],ideo_1012_fr[[2]],ideo_1214_fr[[2]], ideo_1416_fr[[2]], ideo_1618_fr[[2]], 
                                   ideo_1820_fr[[2]]), c(weight1_fr, weight2_fr, weight3_fr, weight4_fr, weight5_fr, weight6_fr))
# tercile 1-2, gender cong 
effect_ideo3_fr <- weighted.mean(c(ideo_0810_fr[[3]],ideo_1012_fr[[3]],ideo_1214_fr[[3]], ideo_1416_fr[[3]], ideo_1618_fr[[3]], 
                                   ideo_1820_fr[[3]]), c(weight1_fr, weight2_fr, weight3_fr, weight4_fr, weight5_fr, weight6_fr))
# tercile 1-3, gender cong 
effect_ideo4_fr <- weighted.mean(c(ideo_0810_fr[[4]],ideo_1012_fr[[4]],ideo_1214_fr[[4]], ideo_1416_fr[[4]], ideo_1618_fr[[4]], 
                                   ideo_1820_fr[[4]]), c(weight1_fr, weight2_fr, weight3_fr, weight4_fr, weight5_fr, weight6_fr))

# Men
# tercile 1-2, gender inc 
effect_ideo1_mr <- weighted.mean(c(ideo_0810_mr[[1]],ideo_1012_mr[[1]],ideo_1214_mr[[1]], ideo_1416_mr[[1]], ideo_1618_mr[[1]], 
                                   ideo_1820_mr[[1]]), c(weight1_mr, weight2_mr, weight3_mr, weight4_mr, weight5_mr, weight6_mr))
# tercile 1-3, gender inc 
effect_ideo2_mr <- weighted.mean(c(ideo_0810_mr[[2]],ideo_1012_mr[[2]],ideo_1214_mr[[2]], ideo_1416_mr[[2]], ideo_1618_mr[[2]], 
                                   ideo_1820_mr[[2]]), c(weight1_mr, weight2_mr, weight3_mr, weight4_mr, weight5_mr, weight6_mr))
# tercile 1-2, gender cong 
effect_ideo3_mr <- weighted.mean(c(ideo_0810_mr[[3]],ideo_1012_mr[[3]],ideo_1214_mr[[3]], ideo_1416_mr[[3]], ideo_1618_mr[[3]], 
                                   ideo_1820_mr[[3]]), c(weight1_mr, weight2_mr, weight3_mr, weight4_mr, weight5_mr, weight6_mr))
# tercile 1-3, gender cong 
effect_ideo4_mr <- weighted.mean(c(ideo_0810_mr[[4]],ideo_1012_mr[[4]],ideo_1214_mr[[4]], ideo_1416_mr[[4]], ideo_1618_mr[[4]], 
                                   ideo_1820_mr[[4]]), c(weight1_mr, weight2_mr, weight3_mr, weight4_mr, weight5_mr, weight6_mr))

# Column of Basic estimate comparison women
basic_row_fr <- cbind(effect_fr, full_basic_fr)
ideo_row1_fr <- cbind(effect_ideo1_fr, full_ideo_fr[[1]])
ideo_row2_fr <- cbind(effect_ideo2_fr, full_ideo_fr[[2]])
ideo_row3_fr <- cbind(effect_ideo3_fr, full_ideo_fr[[3]])
ideo_row4_fr <- cbind(effect_ideo4_fr, full_ideo_fr[[4]])
# bind the rows 
all_effects_fr <- rbind(basic_row_fr, ideo_row1_fr, ideo_row2_fr, ideo_row3_fr,ideo_row4_fr)

# Column of Basic estimate comparison men
basic_row_mr <- cbind(effect_mr, full_basic_mr)
ideo_row1_mr <- cbind(effect_ideo1_mr, full_ideo_mr[[1]])
ideo_row2_mr <- cbind(effect_ideo2_mr, full_ideo_mr[[2]])
ideo_row3_mr <- cbind(effect_ideo3_mr, full_ideo_mr[[3]])
ideo_row4_mr <- cbind(effect_ideo4_mr, full_ideo_mr[[4]])

# bind the rows 
all_effects_mr <- rbind(basic_row_mr, ideo_row1_mr, ideo_row2_mr, ideo_row3_mr, ideo_row4_mr)


# combine men and women 
all_effects_gender.r <- cbind(all_effects_fr, all_effects_mr)

colnames(all_effects_gender.r) <- c("Wtd. Avg. Wom.", "Main Model Wom.", "Wtd. Avg. Men", "Main Model Men")
rownames(all_effects_gender.r) <- c("Bivariate + Controls", 
                                    "Ideology Tercile 1-2 | Gender Incongruence", 
                                    "Ideology Tercile 1-3 | Gender Incongruence", 
                                    "Ideology Tercile 1-2 | Gender Congruence",
                                    "Ideology Tercile 1-3 | Gender Congruence")

stargazer(all_effects_gender.r, type="latex", header = FALSE, title = "Alternative model comparison for Gender Congruence (Republicans)", style = "apsr")

#
# Democratic Respondents Race TABLE ----

## Bivariate Approval on POC MC with controls 
# Create Function to run regression with different datasets and retrieve coefficient 
full_basic_nwd <- bivariate(df_even_nwd, df_even_nwd$race_congruence, df_even_nwd$mc_gender)
basic_0810_nwd <- bivariate(sub_0810_nwd.2, sub_0810_nwd.2$race_congruence, sub_0810_nwd.2$mc_gender)
basic_1012_nwd <- bivariate(sub_1012_nwd.2, sub_1012_nwd.2$race_congruence, sub_1012_nwd.2$mc_gender)
basic_1214_nwd <- bivariate(sub_1214_nwd.2, sub_1214_nwd.2$race_congruence, sub_1214_nwd.2$mc_gender)
basic_1416_nwd <- bivariate(sub_1416_nwd.2, sub_1416_nwd.2$race_congruence, sub_1416_nwd.2$mc_gender)
basic_1618_nwd <- bivariate(sub_1618_nwd.2, sub_1618_nwd.2$race_congruence, sub_1618_nwd.2$mc_gender)
basic_1820_nwd <- bivariate(sub_1820_nwd.2, sub_1820_nwd.2$race_congruence, sub_1820_nwd.2$mc_gender)

full_basic_wd <- bivariate(df_even_wd, df_even_wd$race_congruence, df_even_wd$mc_gender)
basic_0810_wd <- bivariate(sub_0810_wd.2, sub_0810_wd.2$race_congruence, sub_0810_wd.2$mc_gender)
basic_1012_wd <- bivariate(sub_1012_wd.2, sub_1012_wd.2$race_congruence, sub_1012_wd.2$mc_gender)
basic_1214_wd <- bivariate(sub_1214_wd.2, sub_1214_wd.2$race_congruence, sub_1214_wd.2$mc_gender)
basic_1416_wd <- bivariate(sub_1416_wd.2, sub_1416_wd.2$race_congruence, sub_1416_wd.2$mc_gender)
basic_1618_wd <- bivariate(sub_1618_wd.2, sub_1618_wd.2$race_congruence, sub_1618_wd.2$mc_gender)
basic_1820_wd <- bivariate(sub_1820_wd.2, sub_1820_wd.2$race_congruence, sub_1820_wd.2$mc_gender)

# Generate average effect with treatment weights
# POC
effect_nwd <- weighted.mean(c(basic_0810_nwd,basic_1012_nwd,basic_1214_nwd, basic_1416_nwd,basic_1618_nwd,basic_1820_nwd),
                            c(weight1_nwd, weight2_nwd, weight3_nwd, weight4_nwd, weight5_nwd, weight6_nwd))
# WHITE
effect_wd <- weighted.mean(c(basic_0810_wd,basic_1012_wd,basic_1214_wd, basic_1416_wd,basic_1618_wd,basic_1820_wd),
                           c(weight1_wd, weight2_wd, weight3_wd, weight4_wd, weight5_wd, weight6_wd))

## + ideological incongruence
full_ideo_nwd <- fcn.ideo(df_even_nwd, df_even_nwd$race_congruence.fac, df_even_nwd$mc_gender)
ideo_0810_nwd <- fcn.ideo(sub_0810_nwd.2, sub_0810_nwd.2$race_congruence.fac, sub_0810_nwd.2$mc_gender)
ideo_1012_nwd <- fcn.ideo(sub_1012_nwd.2, sub_1012_nwd.2$race_congruence.fac, sub_1012_nwd.2$mc_gender)
ideo_1214_nwd <- fcn.ideo(sub_1214_nwd.2, sub_1214_nwd.2$race_congruence.fac, sub_1214_nwd.2$mc_gender)
ideo_1416_nwd <- fcn.ideo(sub_1416_nwd.2, sub_1416_nwd.2$race_congruence.fac, sub_1416_nwd.2$mc_gender)
ideo_1618_nwd <- fcn.ideo(sub_1618_nwd.2, sub_1618_nwd.2$race_congruence.fac, sub_1618_nwd.2$mc_gender)
ideo_1820_nwd <- fcn.ideo(sub_1820_nwd.2, sub_1820_nwd.2$race_congruence.fac, sub_1820_nwd.2$mc_gender)

full_ideo_wd <- fcn.ideo(df_even_wd, df_even_wd$race_congruence.fac, df_even_wd$mc_gender)
ideo_0810_wd <- fcn.ideo(sub_0810_wd.2, sub_0810_wd.2$race_congruence.fac, sub_0810_wd.2$mc_gender)
ideo_1012_wd <- fcn.ideo(sub_1012_wd.2, sub_1012_wd.2$race_congruence.fac, sub_1012_wd.2$mc_gender)
ideo_1214_wd <- fcn.ideo(sub_1214_wd.2, sub_1214_wd.2$race_congruence.fac, sub_1214_wd.2$mc_gender)
ideo_1416_wd <- fcn.ideo(sub_1416_wd.2, sub_1416_wd.2$race_congruence.fac, sub_1416_wd.2$mc_gender)
ideo_1618_wd <- fcn.ideo(sub_1618_wd.2, sub_1618_wd.2$race_congruence.fac, sub_1618_wd.2$mc_gender)
ideo_1820_wd <- fcn.ideo(sub_1820_wd.2, sub_1820_wd.2$race_congruence.fac, sub_1820_wd.2$mc_gender)

# Generate average effect with treatment weights 
# POC
# tercile 1-2, race inc 
effect_ideo1_nwd <- weighted.mean(c(ideo_0810_nwd[[1]],ideo_1012_nwd[[1]],ideo_1214_nwd[[1]], ideo_1416_nwd[[1]], ideo_1618_nwd[[1]], 
                                    ideo_1820_nwd[[1]]), c(weight1_nwd, weight2_nwd, weight3_nwd, weight4_nwd, weight5_nwd, weight6_nwd))
# tercile 1-3, race inc 
effect_ideo2_nwd <- weighted.mean(c(ideo_0810_nwd[[2]],ideo_1012_nwd[[2]],ideo_1214_nwd[[2]], ideo_1416_nwd[[2]], ideo_1618_nwd[[2]], 
                                    ideo_1820_nwd[[2]]), c(weight1_nwd, weight2_nwd, weight3_nwd, weight4_nwd, weight5_nwd, weight6_nwd))
# tercile 1-2, race cong 
effect_ideo3_nwd <- weighted.mean(c(ideo_0810_nwd[[3]],ideo_1012_nwd[[3]],ideo_1214_nwd[[3]], ideo_1416_nwd[[3]], ideo_1618_nwd[[3]], 
                                    ideo_1820_nwd[[3]]), c(weight1_nwd, weight2_nwd, weight3_nwd, weight4_nwd, weight5_nwd, weight6_nwd))
# tercile 1-3, race cong 
effect_ideo4_nwd <- weighted.mean(c(ideo_0810_nwd[[4]],ideo_1012_nwd[[4]],ideo_1214_nwd[[4]], ideo_1416_nwd[[4]], ideo_1618_nwd[[4]], 
                                    ideo_1820_nwd[[4]]), c(weight1_nwd, weight2_nwd, weight3_nwd, weight4_nwd, weight5_nwd, weight6_nwd))

# White
# tercile 1-2, race inc 
effect_ideo1_wd <- weighted.mean(c(ideo_0810_wd[[1]],ideo_1012_wd[[1]],ideo_1214_wd[[1]], ideo_1416_wd[[1]], ideo_1618_wd[[1]], 
                                   ideo_1820_wd[[1]]), c(weight1_wd, weight2_wd, weight3_wd, weight4_wd, weight5_wd, weight6_wd))
# tercile 1-3, race inc 
effect_ideo2_wd <- weighted.mean(c(ideo_0810_wd[[2]],ideo_1012_wd[[2]],ideo_1214_wd[[2]], ideo_1416_wd[[2]], ideo_1618_wd[[2]], 
                                   ideo_1820_wd[[2]]), c(weight1_wd, weight2_wd, weight3_wd, weight4_wd, weight5_wd, weight6_wd))
# tercile 1-2, race cong 
effect_ideo3_wd <- weighted.mean(c(ideo_0810_wd[[3]],ideo_1012_wd[[3]],ideo_1214_wd[[3]], ideo_1416_wd[[3]], ideo_1618_wd[[3]], 
                                   ideo_1820_wd[[3]]), c(weight1_wd, weight2_wd, weight3_wd, weight4_wd, weight5_wd, weight6_wd))
# tercile 1-3, race cong 
effect_ideo4_wd <- weighted.mean(c(ideo_0810_wd[[4]],ideo_1012_wd[[4]],ideo_1214_wd[[4]], ideo_1416_wd[[4]], ideo_1618_wd[[4]], 
                                   ideo_1820_wd[[4]]), c(weight1_wd, weight2_wd, weight3_wd, weight4_wd, weight5_wd, weight6_wd))

# Column of Basic estimate comparison poc
basic_row_nwd <- cbind(effect_nwd, full_basic_nwd)
ideo_row1_nwd <- cbind(effect_ideo1_nwd, full_ideo_nwd[[1]])
ideo_row2_nwd <- cbind(effect_ideo2_nwd, full_ideo_nwd[[2]])
ideo_row3_nwd <- cbind(effect_ideo3_nwd, full_ideo_nwd[[3]])
ideo_row4_nwd <- cbind(effect_ideo4_nwd, full_ideo_nwd[[4]])
# bind the rows 
all_effects_nwd <- rbind(basic_row_nwd, ideo_row1_nwd, ideo_row2_nwd, ideo_row3_nwd,ideo_row4_nwd)

# Column of Basic estimate comparison white
basic_row_wd <- cbind(effect_wd, full_basic_wd)
ideo_row1_wd <- cbind(effect_ideo1_wd, full_ideo_wd[[1]])
ideo_row2_wd <- cbind(effect_ideo2_wd, full_ideo_wd[[2]])
ideo_row3_wd <- cbind(effect_ideo3_wd, full_ideo_wd[[3]])
ideo_row4_wd <- cbind(effect_ideo4_wd, full_ideo_wd[[4]])

# bind the rows 
all_effects_wd <- rbind(basic_row_wd, ideo_row1_wd, ideo_row2_wd, ideo_row3_wd, ideo_row4_wd)


# combine poc and white 
all_effects_race.d <- cbind(all_effects_nwd, all_effects_wd)

colnames(all_effects_race.d) <- c("Wtd. Avg. POC", "Main Model POC", "Wtd. Avg. White", "Main Model White")
rownames(all_effects_race.d) <- c("Bivariate + Controls", 
                                  "Ideology Tercile 1-2 | Racial Incongruence", 
                                  "Ideology Tercile 1-3 | Racial Incongruence", 
                                  "Ideology Tercile 1-2 | Racial Congruence",
                                  "Ideology Tercile 1-3 | Racial Congruence")

stargazer(all_effects_race.d, type="latex", header = FALSE, title = "Alternative model comparison for Racial Congruence (Democrats)", style = "apsr")

# Republican Respondents Race TABLE ----

## Bivariate Approval on POC MC with controls 
# Create Function to run regression with different datasets and retrieve coefficient 
full_basic_nwr <- bivariate(df_even_nwr, df_even_nwr$race_congruence, df_even_nwr$mc_gender)
basic_0810_nwr <- bivariate(sub_0810_nwr.2, sub_0810_nwr.2$race_congruence, sub_0810_nwr.2$mc_gender)
basic_1012_nwr <- bivariate(sub_1012_nwr.2, sub_1012_nwr.2$race_congruence, sub_1012_nwr.2$mc_gender)
basic_1214_nwr <- bivariate(sub_1214_nwr.2, sub_1214_nwr.2$race_congruence, sub_1214_nwr.2$mc_gender)
basic_1416_nwr <- bivariate(sub_1416_nwr.2, sub_1416_nwr.2$race_congruence, sub_1416_nwr.2$mc_gender)
basic_1618_nwr <- bivariate(sub_1618_nwr.2, sub_1618_nwr.2$race_congruence, sub_1618_nwr.2$mc_gender)
basic_1820_nwr <- bivariate(sub_1820_nwr.2, sub_1820_nwr.2$race_congruence, sub_1820_nwr.2$mc_gender)

full_basic_wr <- bivariate(df_even_wr, df_even_wr$race_congruence, df_even_wr$mc_gender)
basic_0810_wr <- bivariate(sub_0810_wr.2, sub_0810_wr.2$race_congruence, sub_0810_wr.2$mc_gender)
basic_1012_wr <- bivariate(sub_1012_wr.2, sub_1012_wr.2$race_congruence, sub_1012_wr.2$mc_gender)
basic_1214_wr <- bivariate(sub_1214_wr.2, sub_1214_wr.2$race_congruence, sub_1214_wr.2$mc_gender)
basic_1416_wr <- bivariate(sub_1416_wr.2, sub_1416_wr.2$race_congruence, sub_1416_wr.2$mc_gender)
basic_1618_wr <- bivariate(sub_1618_wr.2, sub_1618_wr.2$race_congruence, sub_1618_wr.2$mc_gender)
basic_1820_wr <- bivariate(sub_1820_wr.2, sub_1820_wr.2$race_congruence, sub_1820_wr.2$mc_gender)

# Generate average effect with treatment weights
# poc
effect_nwr <- weighted.mean(c(basic_0810_nwr,basic_1012_nwr,basic_1214_nwr, basic_1416_nwr,basic_1618_nwr,basic_1820_nwr),
                            c(weight1_nwr, weight2_nwr, weight3_nwr, weight4_nwr, weight5_nwr, weight6_nwr))
# white
effect_wr <- weighted.mean(c(basic_0810_wr,basic_1012_wr,basic_1214_wr, basic_1416_wr,basic_1618_wr,basic_1820_wr),
                           c(weight1_wr, weight2_wr, weight3_wr, weight4_wr, weight5_wr, weight6_wr))

## + ideological incongruence
full_ideo_nwr <- fcn.ideo(df_even_nwr, df_even_nwr$race_congruence.fac, df_even_nwr$mc_gender)
ideo_0810_nwr <- fcn.ideo(sub_0810_nwr.2, sub_0810_nwr.2$race_congruence.fac, sub_0810_nwr.2$mc_gender)
ideo_1012_nwr <- fcn.ideo(sub_1012_nwr.2, sub_1012_nwr.2$race_congruence.fac, sub_1012_nwr.2$mc_gender)
ideo_1214_nwr <- fcn.ideo(sub_1214_nwr.2, sub_1214_nwr.2$race_congruence.fac, sub_1214_nwr.2$mc_gender)
ideo_1416_nwr <- fcn.ideo(sub_1416_nwr.2, sub_1416_nwr.2$race_congruence.fac, sub_1416_nwr.2$mc_gender)
ideo_1618_nwr <- fcn.ideo(sub_1618_nwr.2, sub_1618_nwr.2$race_congruence.fac, sub_1618_nwr.2$mc_gender)
ideo_1820_nwr <- fcn.ideo(sub_1820_nwr.2, sub_1820_nwr.2$race_congruence.fac, sub_1820_nwr.2$mc_gender)

full_ideo_wr <- fcn.ideo(df_even_wr, df_even_wr$race_congruence.fac, df_even_wr$mc_gender)
ideo_0810_wr <- fcn.ideo(sub_0810_wr.2, sub_0810_wr.2$race_congruence.fac, sub_0810_wr.2$mc_gender)
ideo_1012_wr <- fcn.ideo(sub_1012_wr.2, sub_1012_wr.2$race_congruence.fac, sub_1012_wr.2$mc_gender)
ideo_1214_wr <- fcn.ideo(sub_1214_wr.2, sub_1214_wr.2$race_congruence.fac, sub_1214_wr.2$mc_gender)
ideo_1416_wr <- fcn.ideo(sub_1416_wr.2, sub_1416_wr.2$race_congruence.fac, sub_1416_wr.2$mc_gender)
ideo_1618_wr <- fcn.ideo(sub_1618_wr.2, sub_1618_wr.2$race_congruence.fac, sub_1618_wr.2$mc_gender)
ideo_1820_wr <- fcn.ideo(sub_1820_wr.2, sub_1820_wr.2$race_congruence.fac, sub_1820_wr.2$mc_gender)

# Generate average effect with treatment weights 
# poc
# tercile 1-2, race inc 
effect_ideo1_nwr <- weighted.mean(c(ideo_0810_nwr[[1]],ideo_1012_nwr[[1]],ideo_1214_nwr[[1]], ideo_1416_nwr[[1]], ideo_1618_nwr[[1]], 
                                    ideo_1820_nwr[[1]]), c(weight1_nwr, weight2_nwr, weight3_nwr, weight4_nwr, weight5_nwr, weight6_nwr))
# tercile 1-3, race inc 
effect_ideo2_nwr <- weighted.mean(c(ideo_0810_nwr[[2]],ideo_1012_nwr[[2]],ideo_1214_nwr[[2]], ideo_1416_nwr[[2]], ideo_1618_nwr[[2]], 
                                    ideo_1820_nwr[[2]]), c(weight1_nwr, weight2_nwr, weight3_nwr, weight4_nwr, weight5_nwr, weight6_nwr))
# tercile 1-2, race cong 
effect_ideo3_nwr <- weighted.mean(c(ideo_0810_nwr[[3]],ideo_1012_nwr[[3]],ideo_1214_nwr[[3]], ideo_1416_nwr[[3]], ideo_1618_nwr[[3]], 
                                    ideo_1820_nwr[[3]]), c(weight1_nwr, weight2_nwr, weight3_nwr, weight4_nwr, weight5_nwr, weight6_nwr))
# tercile 1-3, race cong 
effect_ideo4_nwr <- weighted.mean(c(ideo_0810_nwr[[4]],ideo_1012_nwr[[4]],ideo_1214_nwr[[4]], ideo_1416_nwr[[4]], ideo_1618_nwr[[4]], 
                                    ideo_1820_nwr[[4]]), c(weight1_nwr, weight2_nwr, weight3_nwr, weight4_nwr, weight5_nwr, weight6_nwr))

# white
# tercile 1-2, race inc 
effect_ideo1_wr <- weighted.mean(c(ideo_0810_wr[[1]],ideo_1012_wr[[1]],ideo_1214_wr[[1]], ideo_1416_wr[[1]], ideo_1618_wr[[1]], 
                                   ideo_1820_wr[[1]]), c(weight1_wr, weight2_wr, weight3_wr, weight4_wr, weight5_wr, weight6_wr))
# tercile 1-3, race inc 
effect_ideo2_wr <- weighted.mean(c(ideo_0810_wr[[2]],ideo_1012_wr[[2]],ideo_1214_wr[[2]], ideo_1416_wr[[2]], ideo_1618_wr[[2]], 
                                   ideo_1820_wr[[2]]), c(weight1_wr, weight2_wr, weight3_wr, weight4_wr, weight5_wr, weight6_wr))
# tercile 1-2, race cong 
effect_ideo3_wr <- weighted.mean(c(ideo_0810_wr[[3]],ideo_1012_wr[[3]],ideo_1214_wr[[3]], ideo_1416_wr[[3]], ideo_1618_wr[[3]], 
                                   ideo_1820_wr[[3]]), c(weight1_wr, weight2_wr, weight3_wr, weight4_wr, weight5_wr, weight6_wr))
# tercile 1-3, race cong 
effect_ideo4_wr <- weighted.mean(c(ideo_0810_wr[[4]],ideo_1012_wr[[4]],ideo_1214_wr[[4]], ideo_1416_wr[[4]], ideo_1618_wr[[4]], 
                                   ideo_1820_wr[[4]]), c(weight1_wr, weight2_wr, weight3_wr, weight4_wr, weight5_wr, weight6_wr))

# Column of Basic estimate comparison poc
basic_row_nwr <- cbind(effect_nwr, full_basic_nwr)
ideo_row1_nwr <- cbind(effect_ideo1_nwr, full_ideo_nwr[[1]])
ideo_row2_nwr <- cbind(effect_ideo2_nwr, full_ideo_nwr[[2]])
ideo_row3_nwr <- cbind(effect_ideo3_nwr, full_ideo_nwr[[3]])
ideo_row4_nwr <- cbind(effect_ideo4_nwr, full_ideo_nwr[[4]])
# bind the rows 
all_effects_nwr <- rbind(basic_row_nwr, ideo_row1_nwr, ideo_row2_nwr, ideo_row3_nwr,ideo_row4_nwr)

# Column of Basic estimate comparison white
basic_row_wr <- cbind(effect_wr, full_basic_wr)
ideo_row1_wr <- cbind(effect_ideo1_wr, full_ideo_wr[[1]])
ideo_row2_wr <- cbind(effect_ideo2_wr, full_ideo_wr[[2]])
ideo_row3_wr <- cbind(effect_ideo3_wr, full_ideo_wr[[3]])
ideo_row4_wr <- cbind(effect_ideo4_wr, full_ideo_wr[[4]])

# bind the rows 
all_effects_wr <- rbind(basic_row_wr, ideo_row1_wr, ideo_row2_wr, ideo_row3_wr, ideo_row4_wr)


# combine poc and white 
all_effects_race.r <- cbind(all_effects_nwr, all_effects_wr)

colnames(all_effects_race.r) <- c("Wtd. Avg. POC", "Main Model POC", "Wtd. Avg. White", "Main Model White")
rownames(all_effects_race.r) <- c("Bivariate + Controls", 
                                  "Ideology Tercile 1-2 | Racial Incongruence", 
                                  "Ideology Tercile 1-3 | Racial Incongruence", 
                                  "Ideology Tercile 1-2 | Racial Congruence",
                                  "Ideology Tercile 1-3 | Racial Congruence")

stargazer(all_effects_race.r, type="latex", header = FALSE, title = "Alternative model comparison for Racial Congruence (Republicans)", style = "apsr")

## G. Full Tables for Main Models - ideo other way ----

# Female D respondents 
mod2 <- felm(approval_rep ~ gender_congruence + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_fd, weights = df_even_fd$weight)
# Male D respondents 
mod3 <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_md, weights = df_even_md$weight)
# Female R respondents 
mod4 <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_fr, weights = df_even_fr$weight)
# Male R respondents 
mod5 <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_mr, weights = df_even_mr$weight)

## DW-NOMINATE ideology for gender 
mod2_ideo <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo1:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fd, weights = df_even_fd$weight)
mod3_ideo <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo1:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_md, weights = df_even_md$weight)
mod4_ideo <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo1:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fr, weights = df_even_fr$weight)
mod5_ideo <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo1:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_mr, weights = df_even_mr$weight)

## Perceived ideology for gender 
mod2_p.ideo <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fd, weights = df_even_fd$weight)
mod3_p.ideo <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_md, weights = df_even_md$weight)
mod4_p.ideo <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fr, weights = df_even_fr$weight)
mod5_p.ideo <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_mr, weights = df_even_mr$weight)

# First Democrats 
stargazer(mod2, mod2_ideo, mod2_p.ideo, 
          mod3, mod3_ideo, mod3_p.ideo,
          omit = '[i][n][d]', type='latex', header=FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{Women \\\\ Respondents}", "\\shortstack{Men \\\\ Respondents}"),
          column.separate = c(3, 3),
          #covariate.labels=c("Gender Congruence = 1", "Mid-Level Ideological Congruence = 1", 
          #                   "Ideological Incongruence = 1", "Mid-Level Perceived Congruence = 1", 
          #                   "Perceived Incongruence = 1", "MC Seniority", "POC MC", 
          #                   "Gender Cong. | Ideological Congruence = 1", "Gender Cong. | Mid-Level Ideological Congruence = 1", 
          #                   "Gender Cong. | Ideological Incongruence = 1", "Gender Cong. | Perceived Congruence = 1",
          #                   "Gender Cong. | Mid-Level Perceived Congruence = 1", "Gender Cong. | Perceived Incongruence = 1"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X','X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X','X','X','X','X'),
                         c("N districts w MC Gender change", mod2_t, mod2_t, mod2_t,
                           mod3_t, mod3_t, mod3_t)),
          title = "Effects of Gender Congruence and Substantive Congruence on MC Approval Ratings (Democrats)",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:gender.ideo.dem",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)

# Second Republicans 
stargazer(mod4, mod4_ideo, mod4_p.ideo,
          mod5, mod5_ideo, mod5_p.ideo,
          omit = '[i][n][d]', type='latex', header=FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{Women \\\\ Respondents}", "\\shortstack{Men \\\\ Respondents}"),
          column.separate = c(3, 3),
          #covariate.labels=c("Gender Congruence = 1", "Mid-Level Ideological Congruence = 1", 
          #                   "Ideological Incongruence = 1", "Mid-Level Perceived Congruence = 1", 
          #                   "Perceived Incongruence = 1", "MC Seniority", "POC MC", 
          #                   "Gender Cong. | Ideological Congruence = 1", "Gender Cong. | Mid-Level Ideological Congruence = 1", 
          #                   "Gender Cong. | Ideological Incongruence = 1", "Gender Cong. | Perceived Congruence = 1",
          #                   "Gender Cong. | Mid-Level Perceived Congruence = 1", "Gender Cong. | Perceived Incongruence = 1"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X','X','X'),
                         c("N districts w MC gender change", mod4_t, mod4_t, mod4_t,
                           mod5_t, mod5_t, mod5_t)),
          title = "Effects of Gender Congruence and Substantive Congruence on MC Approval Ratings (Republicans)",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:gender.ideo.rep",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)


# Table for Race 
# Nonwhite D respondents 
mod2.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
# White D respondents 
mod3.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)
# Nonwhite R respondents 
mod4.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
# White R respondents 
mod5.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wr, weights = df_even_wr$weight)

## DW-NOMINATE ideology for nonwhite 
mod2_ideo.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo1:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
mod3_ideo.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo1:race_congruence.fac  + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)
mod4_ideo.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo1:race_congruence.fac  + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
mod5_ideo.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo1:race_congruence.fac  + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wr, weights = df_even_wr$weight)

## Perceived ideology for nonwhite 
mod2_p.ideo.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
mod3_p.ideo.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)
mod4_p.ideo.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
mod5_p.ideo.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wr, weights = df_even_wr$weight)

# First Democrats 
stargazer(mod2.nw, mod2_ideo.nw, mod2_p.ideo.nw,
          mod3.nw, mod3_ideo.nw, mod3_p.ideo.nw,
          omit = '[i][n][d]', type='latex', header=FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{POC \\\\ Respondents}", "\\shortstack{White \\\\ Respondents}"),
          column.separate = c(3, 3),
          #covariate.labels=c("Racial Congruence = 1", "Mid-Level Ideological Congruence = 1", 
          #                   "Ideological Incongruence = 1", "Mid-Level Perceived Congruence = 1", 
          #                   "Perceived Incongruence = 1", "MC Seniority", "MC Gender", 
          #                   "Racial Cong. | Ideological Congruence = 1", "Racial Cong. | Mid-Level Ideological Congruence = 1", 
          #                   "Racial Cong. | Ideological Incongruence = 1", "Racial Cong. | Perceived Congruence = 1",
          #                   "Racial Cong. | Mid-Level Perceived Congruence = 1", "Racial Cong. | Perceived Incongruence = 1"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X','X','X'),
                         c("N districts w MC race change", mod2_t.nw, mod2_t.nw, mod2_t.nw,
                           mod3_t.nw, mod3_t.nw, mod3_t.nw)),
          title = "Effects of Racial Congruence and Substantive Congruence on MC Approval Ratings (Democratics)",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:race.ideo.dem",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)

# Second Republicans 
stargazer(mod4.nw, mod4_ideo.nw, mod4_p.ideo.nw,
          mod5.nw, mod5_ideo.nw, mod5_p.ideo.nw,
          omit = '[i][n][d]', type='latex', header=FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{POC \\\\ Respondents}", "\\shortstack{White \\\\ Respondents}"),
          column.separate = c(3, 3),
          #covariate.labels=c("Racial Congruence = 1", "Mid-Level Ideological Congruence = 1", 
          #                   "Ideological Incongruence = 1", "Mid-Level Perceived Congruence = 1", 
          #                   "Perceived Incongruence = 1", "MC Seniority", "MC Gender", 
          #                   "Racial Cong. | Ideological Congruence = 1", "Racial Cong. | Mid-Level Ideological Congruence = 1", 
          #                   "Racial Cong. | Ideological Incongruence = 1", "Racial Cong. | Perceived Congruence = 1",
          #                   "Racial Cong. | Mid-Level Perceived Congruence = 1", "Racial Cong. | Perceived Incongruence = 1"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X','X','X'),
                         c("N districts w MC race change", mod4_t.nw, mod4_t.nw, mod4_t.nw,
                           mod5_t.nw, mod5_t.nw, mod5_t.nw)),
          title = "Effects of Racial Congruence and Substantive Congruence on MC Approval Ratings (Republicans)",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:race.ideo.rep",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)


# Main Models - no controls, no FEs ----

# GENDER - no controls, with FEs
mod2_biv <- felm(approval_rep ~ gender_congruence | cong + dist_party_mc | 0 | state_dist, data = df_even_fd, weights = df_even_fd$weight)
# Male D respondents 
mod3_biv <- felm(approval_rep ~ gender_congruence | cong + dist_party_mc | 0 | state_dist, data = df_even_md, weights = df_even_md$weight)
# Female R respondents 
mod4_biv <- felm(approval_rep ~ gender_congruence | cong + dist_party_mc | 0 | state_dist, data = df_even_fr, weights = df_even_fr$weight)
# Male R respondents 
mod5_biv <- felm(approval_rep ~ gender_congruence | cong + dist_party_mc | 0 | state_dist, data = df_even_mr, weights = df_even_mr$weight)

# GENDER - no controls, no FEs
mod2_biv.n <- felm(approval_rep ~ gender_congruence | 0 | 0 | state_dist, data = df_even_fd, weights = df_even_fd$weight)
# Male D respondents 
mod3_biv.n <- felm(approval_rep ~ gender_congruence | 0 | 0 | state_dist, data = df_even_md, weights = df_even_md$weight)
# Female R respondents 
mod4_biv.n <- felm(approval_rep ~ gender_congruence | 0 | 0 | state_dist, data = df_even_fr, weights = df_even_fr$weight)
# Male R respondents 
mod5_biv.n <- felm(approval_rep ~ gender_congruence | 0 | 0 | state_dist, data = df_even_mr, weights = df_even_mr$weight)


# RACE - no controls, with FEs
# Nonwhite D respondents 
mod2_biv.nw <- felm(approval_rep ~ race_congruence | cong + dist_party_mc | 0 | state_dist, data = df_even_fd, weights = df_even_fd$weight)
# White D respondents 
mod3_biv.nw <- felm(approval_rep ~ race_congruence | cong + dist_party_mc | 0 | state_dist, data = df_even_md, weights = df_even_md$weight)
# Nonwhite R respondents 
mod4_biv.nw <- felm(approval_rep ~ race_congruence | cong + dist_party_mc | 0 | state_dist, data = df_even_fr, weights = df_even_fr$weight)
# White R respondents 
mod5_biv.nw <- felm(approval_rep ~ race_congruence | cong + dist_party_mc | 0 | state_dist, data = df_even_mr, weights = df_even_mr$weight)

# RACE - no controls, no FEs
# Nonwhite D respondents 
mod2_biv.nw.n <- felm(approval_rep ~ race_congruence | 0 | 0 | state_dist, data = df_even_fd, weights = df_even_fd$weight)
# White D respondents 
mod3_biv.nw.n <- felm(approval_rep ~ race_congruence | 0 | 0 | state_dist, data = df_even_md, weights = df_even_md$weight)
# Nonwhite R respondents 
mod4_biv.nw.n <- felm(approval_rep ~ race_congruence | 0 | 0 | state_dist, data = df_even_fr, weights = df_even_fr$weight)
# White R respondents 
mod5_biv.nw.n <- felm(approval_rep ~ race_congruence | 0 | 0 | state_dist, data = df_even_mr, weights = df_even_mr$weight)

stargazer(mod2_biv, mod2_biv.n, mod3_biv, mod3_biv.n,
          mod4_biv, mod4_biv.n, mod5_biv, mod5_biv.n,
          omit = c('[i][n][d]'), type='latex', header=FALSE, style="apsr",
          keep = "gender_congruence",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{(1) D Women \\\\ Respondents}", "\\shortstack{(2) D Men \\\\ Respondents}", 
                            "\\shortstack{(3) R Women \\\\ Respondents}", "\\shortstack{(4) R Men \\\\ Respondents}"),
          column.separate = c(2,2,2,2),
          covariate.labels=c("Gender Congruence = 1"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','','X','','X','','X',''),
                         c('MC Party x Dist FE', 'X','','X','','X','','X',''),
                         c("N districts w MC gender change", mod2_t,mod2_t, mod3_t,mod3_t, mod4_t,mod4_t, mod5_t,mod5_t)),
          title = "Effects of MC-Constituent Gender Congruence on MC Approval Ratings - No Controls, No FEs",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:gender",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001", 
                    ""), notes.append = F)


stargazer(mod2_biv.nw, mod2_biv.nw.n, mod3_biv.nw, mod3_biv.nw.n,
          mod4_biv.nw, mod4_biv.nw.n, mod5_biv.nw, mod5_biv.nw.n,
          omit = c('[i][n][d]'), type='latex', header=FALSE, style="apsr",
          keep = "race_congruence",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{(1) D POC \\\\ Respondents}", "\\shortstack{(2) D White \\\\ Respondents}", 
                            "\\shortstack{(3) R POC \\\\ Respondents}", "\\shortstack{(4) R White \\\\ Respondents}"),
          column.separate = c(2,2,2,2),
          covariate.labels=c("Race Congruence = 1"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','','X','','X','','X',''),
                         c('MC Party x Dist FE', 'X','','X','','X','','X',''),
                         c("N districts w MC gender change", mod2_t.nw,mod2_t.nw, mod3_t.nw,mod3_t.nw, 
                           mod4_t.nw,mod4_t.nw, mod5_t.nw,mod5_t.nw)),
          title = "Effects of MC-Constituent Racial Congruence on MC Approval Ratings - No Controls, No FEs",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:race",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001", 
                    ""), notes.append = F)

# Equality of Coefficients between women and men/POC and whites ----
# Female D respondents 
mod2 <- felm(approval_rep ~ gender_congruence + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_fd, weights = df_even_fd$weight)
# Male D respondents 
mod3 <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_md, weights = df_even_md$weight)
# Female R respondents 
mod4 <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_fr, weights = df_even_fr$weight)
# Male R respondents 
mod5 <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_mr, weights = df_even_mr$weight)

# Equality of coefficients for basic race models 
# Nonwhite D respondents 
mod2.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
# White D respondents 
mod3.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)
# Nonwhite R respondents 
mod4.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
# White R respondents 
mod5.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wr, weights = df_even_wr$weight)

ztest <- function(model1, model2, number1){
  Z <- (model1$coefficients[[number1]] - model2$coefficients[[number1]])/(sqrt((model1$se[[number1]])^2 + (model2$se[[number1]])^2))
  pval <- 2*pnorm(-abs(Z))
  Eq.coeff <- rbind(Z, pval)
  Eq.coeff
}

ztests.base <- data.frame()
ztests.base[1,1] <- "Gender Congruence"
ztests.base[2,1] <- "Racial Congruence"

# D women vs. R women 
ztests.base[1,2] <- ztest(mod2, mod4, 1)[[1]] 
ztests.base[1,3] <- ztest(mod2, mod4, 1)[[2]] 
# D men vs. R men 
ztests.base[1,4] <- ztest(mod3, mod5, 1)[[1]] 
ztests.base[1,5] <- ztest(mod3, mod5, 1)[[2]] 

# D poc vs. R poc 
ztests.base[2,2] <- ztest(mod2.nw, mod4.nw, 1)[[1]] 
ztests.base[2,3] <- ztest(mod2.nw, mod4.nw, 1)[[2]] 
# D white vs. R white 
ztests.base[2,4] <- ztest(mod3.nw, mod5.nw, 1)[[1]] 
ztests.base[2,5] <- ztest(mod3.nw, mod5.nw, 1)[[2]] 

colnames(ztests.base) <- c("Identity Group", "D vs. R Z-score Women/POC", "P-Val. 1", "D vs. R Z-score Men/Whites", "P-Val. 2")

ztests.base[,2] <- round(ztests.base[,2],3)
ztests.base[,3] <- round(ztests.base[,3],3)
ztests.base[,4] <- round(ztests.base[,4],3)
ztests.base[,5] <- round(ztests.base[,5],3)

ztests.base <- as.matrix(ztests.base)

stargazer(ztests.base, type = "latex", title="Equality of Coefficients - Party Difference in Effect of MC Identity Congruence on Approval", 
          header=F,style="apsr",
          colnames = TRUE, rownames = FALSE, 
          column.sep.width = ".5pt", font.size="scriptsize")

# Function for the ztest with absolute value
ztest.abs <- function(model1, model2, number){
  Z <- (abs(model1$coefficients[[number]]) - abs(model2$coefficients[[number]]))/(sqrt((model1$se[[number]])^2 + (model2$se[[number]])^2))
  pval <- 2*pnorm(-abs(Z))
  Eq.coeff <- rbind(Z, pval)
  Eq.coeff
}

ztests.group <- data.frame()
ztests.group[1,1] <- "Gender Congruence"
ztests.group[2,1] <- "Racial Congruence"

## D men vs. D women 
ztests.group[1,2] <- ztest.abs(mod2, mod3, 1)[[1]] 
ztests.group[1,3] <- ztest.abs(mod2, mod3, 1)[[2]] 
# R women vs. R men 
ztests.group[1,4] <- ztest.abs(mod4, mod5, 1)[[1]] 
ztests.group[1,5] <- ztest.abs(mod4, mod5, 1)[[2]] 

# D poc vs. D white 
ztests.group[2,2] <- ztest.abs(mod2.nw, mod3.nw, 1)[[1]] 
ztests.group[2,3] <- ztest.abs(mod2.nw, mod3.nw, 1)[[2]] 
# R poc vs. R white 
ztests.group[2,4] <- ztest.abs(mod4.nw, mod5.nw, 1)[[1]] 
ztests.group[2,5] <- ztest.abs(mod4.nw, mod5.nw, 1)[[2]] 

colnames(ztests.group) <- c("Identity Group", "Wom./POC vs. Men/White Z-score D", "P-Val. 1", "Wom./POC vs. Men/White Z-score R", "P-Val. 2")

ztests.group[,2] <- round(ztests.group[,2],3)
ztests.group[,3] <- round(ztests.group[,3],3)
ztests.group[,4] <- round(ztests.group[,4],3)
ztests.group[,5] <- round(ztests.group[,5],3)

ztests.group <- as.matrix(ztests.group)

stargazer(ztests.group, type = "latex", title="Equality of Coefficients - Subgroup Absolute Differences in Effect of MC Identity Congruence on Approval", digits=3, header=F,
          colnames = TRUE, rownames = FALSE,style = "apsr",
          column.sep.width = ".5pt", font.size="scriptsize")

# Equality of coefficients ----
ztest.samemod <- function(model1, number1, number2){
  Z <- (model1$coefficients[[number1]] - model1$coefficients[[number2]])/(sqrt((model1$se[[number1]])^2 + (model1$se[[number2]])^2))
  pval <- 2*pnorm(-abs(Z))
  Eq.coeff <- rbind(Z, pval)
  Eq.coeff
}

# Tables G9 and G10 - equality of coefficients between identity congruent and incongruent 

ztests.g <- data.frame()
ztests.g[1,1] <- "Democratic women"
ztests.g[2,1] <- "Democratic Men"
ztests.g[3,1] <- "Republican Women"
ztests.g[4,1] <- "Republican Men"

# D women vs. R women 
ztests.g[1,2] <- ztest.samemod(mod2_p.ideo, 4, 5)[[1]] 
ztests.g[1,3] <- ztest.samemod(mod2_p.ideo, 4, 5)[[2]] 
ztests.g[1,4] <- ztest.samemod(mod2_p.ideo, 6, 7)[[1]] 
ztests.g[1,5] <- ztest.samemod(mod2_p.ideo, 6, 7)[[2]]
ztests.g[2,2] <- ztest.samemod(mod3_p.ideo, 4, 5)[[1]] 
ztests.g[2,3] <- ztest.samemod(mod3_p.ideo, 4, 5)[[2]]
ztests.g[2,4] <- ztest.samemod(mod3_p.ideo, 6, 7)[[1]] 
ztests.g[2,5] <- ztest.samemod(mod3_p.ideo, 6, 7)[[2]] 
ztests.g[3,2] <- ztest.samemod(mod4_p.ideo, 4, 5)[[1]] 
ztests.g[3,3] <- ztest.samemod(mod4_p.ideo, 4, 5)[[2]] 
ztests.g[3,4] <- ztest.samemod(mod4_p.ideo, 6, 7)[[1]] 
ztests.g[3,5] <- ztest.samemod(mod4_p.ideo, 6, 7)[[2]] 
ztests.g[4,2] <- ztest.samemod(mod5_p.ideo, 4, 5)[[1]] 
ztests.g[4,3] <- ztest.samemod(mod5_p.ideo, 4, 5)[[2]] 
ztests.g[4,4] <- ztest.samemod(mod5_p.ideo, 6, 7)[[1]]
ztests.g[4,5] <- ztest.samemod(mod5_p.ideo, 6, 7)[[2]]

colnames(ztests.g) <- c("Respondents", "Ideo 1-2, Cong vs. Incong.", "P-Val 1", "Ideo 1-3, Cong vs. Incong.", "P-Val 2")
ztests.g[,2:5] <- round(ztests.g[,2:5],3)
ztests.g <- as.matrix(ztests.g)

stargazer(ztests.g, type = "latex", title="Equality of Coefficients - Effect of Ideological Incongruence on Approval for Gender Congruent and Incongruent", 
          digits=3, header=F, style="apsr",
          colnames = TRUE, rownames = FALSE,
          column.sep.width = ".5pt", font.size="scriptsize")

# Race equality of coefficients 
ztests.r <- data.frame()
ztests.r[1,1] <- "Democratic POC"
ztests.r[2,1] <- "Democratic Whites"
ztests.r[3,1] <- "Republican POC"
ztests.r[4,1] <- "Republican Whites"

# D women vs. R women 
ztests.r[1,2] <- ztest.samemod(mod2_p.ideo.nw, 4, 5)[[1]] 
ztests.r[1,3] <- ztest.samemod(mod2_p.ideo.nw, 4, 5)[[2]] 
ztests.r[1,4] <- ztest.samemod(mod2_p.ideo.nw, 6, 7)[[1]] 
ztests.r[1,5] <- ztest.samemod(mod2_p.ideo.nw, 6, 7)[[2]]
ztests.r[2,2] <- ztest.samemod(mod3_p.ideo.nw, 4, 5)[[1]] 
ztests.r[2,3] <- ztest.samemod(mod3_p.ideo.nw, 4, 5)[[2]]
ztests.r[2,4] <- ztest.samemod(mod3_p.ideo.nw, 6, 7)[[1]] 
ztests.r[2,5] <- ztest.samemod(mod3_p.ideo.nw, 6, 7)[[2]] 
ztests.r[3,2] <- ztest.samemod(mod4_p.ideo.nw, 4, 5)[[1]] 
ztests.r[3,3] <- ztest.samemod(mod4_p.ideo.nw, 4, 5)[[2]] 
ztests.r[3,4] <- ztest.samemod(mod4_p.ideo.nw, 6, 7)[[1]] 
ztests.r[3,5] <- ztest.samemod(mod4_p.ideo.nw, 6, 7)[[2]] 
ztests.r[4,2] <- ztest.samemod(mod5_p.ideo.nw, 4, 5)[[1]] 
ztests.r[4,3] <- ztest.samemod(mod5_p.ideo.nw, 4, 5)[[2]] 
ztests.r[4,4] <- ztest.samemod(mod5_p.ideo.nw, 6, 7)[[1]]
ztests.r[4,5] <- ztest.samemod(mod5_p.ideo.nw, 6, 7)[[2]]

colnames(ztests.r) <- c("Respondents", "Ideo 1-2, Cong vs. Incong.", "P-Val 1", "Ideo 1-3, Cong vs. Incong.", "P-Val 2")
ztests.r[,2:5] <- round(ztests.r[,2:5],3)
ztests.r <- as.matrix(ztests.r)
stargazer(ztests.r, type = "latex", title="Equality of Coefficients - Effect of Ideological Incongruence on Approval for Racial Congruent and Incongruent MCs", 
          digits=3, header=F,
          colnames = TRUE, rownames = FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize")

## H. Year-by-year Effects of MC Identity on Approval ----
# yearly models from main paper

# Manually move things around in overleaf, but all same values 
stargazer(mod1.yearly, mod2.yearly, mod3.yearly, mod4.yearly,
          mod1.yearly.nw, mod2.yearly.nw, mod3.yearly.nw, mod4.yearly.nw,
          omit = '[i][n][d]', type='latex', header=FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("D Women", "D Men", "R Women", "R Men", "D POC", "D White", "R POC", "R White"),
          #covariate.labels=c("2010", "2012", "2014", "2016", "2018", "2020", "MC Seniority", "MC Race", 
          #                   "Gender Cong. | 2008", "Gender Cong. | 2010", "Gender Cong. | 2012",
          #                   "Gender Cong. | 2014", "Gender Cong. | 2016", "Gender Cong. | 2018", 
          #                   "Gender Cong. | 2020", "MC Gender", "Racial Cong. | 2008", "Racial Cong. | 2010", 
          #                   "Racial Cong. | 2012", "Racial Cong. | 2014", "Racial Cong. | 2016", 
          #                   "Racial Cong. | 2018", "Racial Cong. | 2020"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('MC Party x Dist FE', 'X','X','X','X','X','X','X','X'),
                         c('Congressional Session FE', 'X','X','X','X','X','X','X','X')),
          #c("N districts w MC race change", mod2_t.nw, mod3_t.nw, mod4_t.nw, mod5_t.nw)),
          title = "Effects of MC Identity on MC Approval Ratings by Congressional Session",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:POC",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)

# H2. D with D vs R MCs ----

# Yearly men
df_even_md.d <- df_even_md %>% filter(mc_party==1)
df_even_md.r <- df_even_md %>% filter(mc_party==0)

# D MC Democratic Men respondent model by year 
mod1.Dmd <- felm(approval_rep ~ factor(year) + mc_seniority + nonwhite_mc + gender_congruence:factor(year) | 
                   state_dist | 0 | state_dist, data = df_even_md.d, weights = df_even_md.d$weight)

# R MC Democratic Men respondent model by year 
mod2.Rmd <- felm(approval_rep ~ factor(year) + mc_seniority + nonwhite_mc + gender_congruence:factor(year) | 
                   state_dist | 0 | state_dist, data = df_even_md.r, weights = df_even_md.r$weight)

# Yearly whites
df_even_wd.d <- df_even_wd %>% filter(mc_party==1)
df_even_wd.r <- df_even_wd %>% filter(mc_party==0)

# Democratic White respondent model by year 
mod3.Dwd <- felm(approval_rep ~ factor(year) + mc_seniority + mc_gender + race_congruence:factor(year) | 
                   state_dist | 0 | state_dist, data = df_even_wd.d, weights = df_even_wd.d$weight)
# Republican White respondent model by year 
mod4.Rwd <- felm(approval_rep ~ factor(year) + mc_seniority + mc_gender + race_congruence:factor(year) | 
                   state_dist | 0 | state_dist, data = df_even_wd.r, weights = df_even_wd.r$weight)

# Calculate number of treated districts in each dataset
treat2 <- df_even_md.d %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod2_t <- length(which(treat2$treated==1))

treat3 <- df_even_md.r %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod3_t <- length(which(treat3$treated==1))

treat4 <- df_even_wd.d %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_race = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_race > 0 & pct_race < 1 ~ 1, TRUE ~ 0))
mod4_t <- length(which(treat4$treated==1))

treat5 <- df_even_wd.r %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_race = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_race > 0 & pct_race < 1 ~ 1, TRUE ~ 0))
mod5_t <- length(which(treat5$treated==1))

# put both in table to compare
stargazer(mod1.Dmd, mod2.Rmd, mod3.Dwd, mod4.Rwd,
          omit = c('[i][n][d]', "mc_seniority", "nonwhite_mc", "mc_gender"), 
          type='latex', header=FALSE, style="ajps",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{D MC \\\\ Men}", "\\shortstack{R MC \\\\ Men}",
                            "\\shortstack{D MC \\\\ Whites}", "\\shortstack{R MC \\\\ Whites}"),
          #covariate.labels=c("Gender Congruence = 1", "Race Congruence = 1"),# "MC Seniority", "POC MC = 1"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X'),
                         c("N districts w MC identity change", 
                           mod2_t, mod3_t, mod4_t, mod5_t)),
          #title = "Effects of MC-Constituent Gender Congruence on MC Approval Ratings",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:gender",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)
#
# H3. R with D vs R MCs ----

# Merge together into table in overleaf manually

# Yearly men
df_even_mr.d <- df_even_mr %>% filter(mc_party==1)
df_even_mr.r <- df_even_mr %>% filter(mc_party==0)

# D MC Democratic Men respondent model by year 
mod1.Dmr <- felm(approval_rep ~ factor(year) + mc_seniority + nonwhite_mc + gender_congruence:factor(year) | 
                   state_dist | 0 | state_dist, data = df_even_mr.d, weights = df_even_mr.d$weight)

# R MC Democratic Men respondent model by year 
mod2.Rmr <- felm(approval_rep ~ factor(year) + mc_seniority + nonwhite_mc + gender_congruence:factor(year) | 
                   state_dist | 0 | state_dist, data = df_even_mr.r, weights = df_even_mr.r$weight)

# Yearly whites
df_even_wr.d <- df_even_wr %>% filter(mc_party==1)
df_even_wr.r <- df_even_wr %>% filter(mc_party==0)

# Democratic White respondent model by year 
mod3.Dwr <- felm(approval_rep ~ factor(year) + mc_seniority + mc_gender + race_congruence:factor(year) | 
                   state_dist | 0 | state_dist, data = df_even_wr.d, weights = df_even_wr.d$weight)
# Republican White respondent model by year 
mod4.Rwr <- felm(approval_rep ~ factor(year) + mc_seniority + mc_gender + race_congruence:factor(year) | 
                   state_dist | 0 | state_dist, data = df_even_wr.r, weights = df_even_wr.r$weight)

# Calculate number of treated districts in each dataset
treat2 <- df_even_mr.d %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod2_tr <- length(which(treat2$treated==1))

treat3 <- df_even_mr.r %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod3_tr <- length(which(treat3$treated==1))

treat4 <- df_even_wr.d %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_race = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_race > 0 & pct_race < 1 ~ 1, TRUE ~ 0))
mod4_tr <- length(which(treat4$treated==1))

treat5 <- df_even_wr.r %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_race = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_race > 0 & pct_race < 1 ~ 1, TRUE ~ 0))
mod5_tr <- length(which(treat5$treated==1))

# put both in table to compare
stargazer(mod1.Dmd, mod2.Rmd, mod3.Dwd, mod4.Rwd,
          mod1.Dmr, mod2.Rmr, mod3.Dwr, mod4.Rwr,
          omit = c('[i][n][d]', "mc_seniority", "nonwhite_mc", "mc_gender"), 
          type='latex', header=FALSE, style="ajps",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels= c(""),
          column.labels = c("\\shortstack{D MC \\\\ Men}", "\\shortstack{R MC \\\\ Men}",
                            "\\shortstack{D MC \\\\ Whites}", "\\shortstack{R MC \\\\ Whites}",
                            "\\shortstack{D MC \\\\ Men}", "\\shortstack{R MC \\\\ Men}",
                            "\\shortstack{D MC \\\\ Whites}", "\\shortstack{R MC \\\\ Whites}"),
          #covariate.labels=c("Gender Congruence = 1", "Race Congruence = 1"),# "MC Seniority", "POC MC = 1"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Respondent Party', "D", "D", "D", "D", "R", "R", "R", "R"),
                         c('Dist FE', 'X','X','X','X','X','X','X','X'),
                         c("N districts w MC identity change", 
                           mod2_t, mod3_t, mod4_t, mod5_t)),
          #title = "Effects of MC-Constituent Gender Congruence on MC Approval Ratings",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:gender",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)
#
## I. Direction of ideological misperception ----

# Score is (MC ideo - self ideo) and 1 is conservative and 0 is liberal 
# 1-0 = 1, so MC is more conservative if score is positive 
# 0-1 = -1, so MC is more liberal if score is negative 
# 0-0=0, so MC is congruent if score is 0

# Restrict to those who are negative and those who are positive 

# Democratic women 
df_even_fd.MClib <- df_even_fd %>%
  filter(incongruence_ideo2.2 < 0) # more liberal

df_even_fd.MCcons <- df_even_fd %>%
  filter(incongruence_ideo2.2 > 0) # more conservative 

mod.ideo1 <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fd.MClib, weights = df_even_fd.MClib$weight)
mod.ideo2 <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_fd.MCcons, weights = df_even_fd.MCcons$weight)

# Democratic men 
df_even_md.MClib <- df_even_md %>%
  filter(incongruence_ideo2.2 < 0) # more liberal

df_even_md.MCcons <- df_even_md %>%
  filter(incongruence_ideo2.2 > 0) # more conservative

mod.ideo1.men <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_md.MClib, weights = df_even_md.MClib$weight)
mod.ideo2.men <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_md.MCcons, weights = df_even_md.MCcons$weight)

# Democratic POC 
df_even_wd.MClib <- df_even_wd %>%
  filter(incongruence_ideo2.2 < 0)

df_even_wd.MCcons <- df_even_wd %>%
  filter(incongruence_ideo2.2 > 0)

mod.ideo1.white <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd.MClib, weights = df_even_wd.MClib$weight)
mod.ideo2.white <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd.MCcons, weights = df_even_wd.MCcons$weight)

# Democratic white 
df_even_nwd.MClib <- df_even_nwd %>%
  filter(incongruence_ideo2.2 < 0)

df_even_nwd.MCcons <- df_even_nwd %>%
  filter(incongruence_ideo2.2 > 0)

mod.ideo1.poc <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwd.MClib, weights = df_even_nwd.MClib$weight)
mod.ideo2.poc <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwd.MCcons, weights = df_even_nwd.MCcons$weight)

stargazer(mod.ideo1, mod.ideo1.men, mod.ideo1.poc, mod.ideo1.white, 
          mod.ideo2, mod.ideo2.men,mod.ideo2.poc, mod.ideo2.white, 
          omit = '[i][n][d]', type='latex', header=FALSE, style="ajps",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{MC more \\\\ Liberal}", "\\shortstack{MC more \\\\ Conservative}"),
          column.separate = c(4, 4),
          omit.stat=c("f", "ser", "rsq"),
          add.lines=list(c('Respondent Identity', 'Woman','Man','POC','White','Woman','Man','POC','White'),
                         c('Congress FE', 'X','X','X','X','X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X','X','X','X','X')),
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:gender.ideo.dem",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)

## J. Table: Effect of Perceived Ideological Congruence all else equal Tables ----

# Bivariate perceived Ideology 
# Female D respondents 
mod2_biv_pideo <- felm(approval_rep ~ inc_tercile_ideo2 + nonwhite_mc + mc_seniority  | cong + dist_party_mc | 0 | 
                         state_dist, data = df_even_fd, weights = df_even_fd$weight)
# Male D respondents 
mod3_biv_pideo <- felm(approval_rep ~ inc_tercile_ideo2 + nonwhite_mc + mc_seniority  | cong + dist_party_mc | 0 | 
                         state_dist, data = df_even_md, weights = df_even_md$weight)
# Female R respondents 
mod4_biv_pideo <- felm(approval_rep ~ inc_tercile_ideo2 + nonwhite_mc + mc_seniority  | cong + dist_party_mc | 0 | 
                         state_dist, data = df_even_fr, weights = df_even_fr$weight)
# Male R respondents 
mod5_biv_pideo <- felm(approval_rep ~ inc_tercile_ideo2 + nonwhite_mc + mc_seniority  | cong + dist_party_mc | 0 | 
                         state_dist, data = df_even_mr, weights = df_even_mr$weight)

## RACE 
# Bivariate perceived Ideology 
# poc D respondents 
mod2_biv_pideo.nw <- felm(approval_rep ~ inc_tercile_ideo2 + mc_gender + mc_seniority | cong + dist_party_mc | 0 | 
                            state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
# white D respondents 
mod3_biv_pideo.nw <- felm(approval_rep ~ inc_tercile_ideo2 + mc_gender + mc_seniority | cong + dist_party_mc | 0 | 
                            state_dist, data = df_even_wd, weights = df_even_wd$weight)
# poc R respondents 
mod4_biv_pideo.nw <- felm(approval_rep ~ inc_tercile_ideo2 + mc_gender + mc_seniority | cong + dist_party_mc | 0 | 
                            state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
# white R respondents 
mod5_biv_pideo.nw <- felm(approval_rep ~ inc_tercile_ideo2 + mc_gender + mc_seniority | cong + dist_party_mc | 0 | 
                            state_dist, data = df_even_wr, weights = df_even_wr$weight)

stargazer(mod2_biv_pideo, mod3_biv_pideo, mod4_biv_pideo, mod5_biv_pideo,
          mod2_biv_pideo.nw, mod3_biv_pideo.nw, mod4_biv_pideo.nw, mod5_biv_pideo.nw,
          omit = c('[i][n][d]'),#,"nonwhite_mc","mc_seniority"), 
          type='latex', header=FALSE, style="ajps",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = TRUE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{D Women \\\\ Respondents}", "\\shortstack{D Men \\\\ Respondents}",
                            "\\shortstack{R Women \\\\ Respondents}", "\\shortstack{R Men \\\\ Respondents}",
                            "\\shortstack{D POC \\\\ Respondents}", "\\shortstack{D White \\\\ Respondents}",
                            "\\shortstack{R POC \\\\ Respondents}", "\\shortstack{R White \\\\ Respondents}"),
          #column.separate = c(2, 2, 2, 2),
          #covariate.labels=c("Mid-Level Ideo. Congruence", "Ideo. Incongruence ",
          #                   "Mid-Level Perceived Congruence", "Perceived Incongruence",
          #                   "MC Race", "MC Seniority"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X','X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X','X','X','X','X')),
          title = "Effects of Ideological Congruence on MC Approval Ratings",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)

#
## J. Figure: Knowledge and ideological congruence ----
# clean up variable 
df_even.knowledge <- df_even2 %>%
  select(approval_rep, inc_tercile_ideo2, nonwhite_mc, mc_seniority, mc_gender, 
         cong, dist_party_mc, weight, state_dist, knowledge2, gender, nonwhite, party7)

# Female Dems 
df_even_fd.knowlow <- df_even.knowledge %>%
  filter(party7 == 1 & gender==1 & knowledge2 == 0)
df_even_fd.knowhigh <- df_even.knowledge %>%
  filter(party7 == 1 & gender==1 & knowledge2 == 1)
# Male Dems 
df_even_md.knowlow <- df_even.knowledge %>%
  filter(party7 == 1 & gender==0 & knowledge2 == 0)
df_even_md.knowhigh <- df_even.knowledge %>%
  filter(party7 == 1 & gender==0 & knowledge2 == 1)
# Female GOP
df_even_fr.knowlow <- df_even.knowledge %>%
  filter(party7 == 3 & gender==1 & knowledge2 == 0)
df_even_fr.knowhigh <- df_even.knowledge %>%
  filter(party7 == 3 & gender==1 & knowledge2 == 1)
# Male GOP 
df_even_mr.knowlow <- df_even.knowledge %>%
  filter(party7 == 3 & gender==0 & knowledge2 == 0)
df_even_mr.knowhigh <- df_even.knowledge %>%
  filter(party7 == 3 & gender==0 & knowledge2 == 1)

# Nonwhite Dems 
df_even_nwd.knowlow <- df_even.knowledge %>%
  filter(party7 == 1 & nonwhite==1 & knowledge2 == 0)
df_even_nwd.knowhigh <- df_even.knowledge %>%
  filter(party7 == 1 & nonwhite==1 & knowledge2 == 1)
# White Dems 
df_even_wd.knowlow <- df_even.knowledge %>%
  filter(party7 == 1 & nonwhite==0 & knowledge2 == 0)
df_even_wd.knowhigh <- df_even.knowledge %>%
  filter(party7 == 1 & nonwhite==0 & knowledge2 == 1)
# Nonwhite GOP
df_even_nwr.knowlow <- df_even.knowledge %>%
  filter(party7 == 3 & nonwhite==1 & knowledge2 == 0)
df_even_nwr.knowhigh <- df_even.knowledge %>%
  filter(party7 == 3 & nonwhite==1 & knowledge2 == 1)
# White GOP 
df_even_wr.knowlow <- df_even.knowledge %>%
  filter(party7 == 3 & nonwhite==0 & knowledge2 == 0)
df_even_wr.knowhigh <- df_even.knowledge %>%
  filter(party7 == 3 & nonwhite==0 & knowledge2 == 1)

# Female D respondents 
mod2_biv_knowlow <- felm(approval_rep ~ inc_tercile_ideo2 + nonwhite_mc + mc_seniority  | cong + dist_party_mc | 0 | 
                           state_dist, data = df_even_fd.knowlow, weights = df_even_fd.knowlow$weight)
mod2_biv_knowhigh <- felm(approval_rep ~ inc_tercile_ideo2 + nonwhite_mc + mc_seniority  | cong + dist_party_mc | 0 | 
                            state_dist, data = df_even_fd.knowhigh, weights = df_even_fd.knowhigh$weight)
# Male D respondents 
mod3_biv_knowlow <- felm(approval_rep ~ inc_tercile_ideo2 + nonwhite_mc + mc_seniority  | cong + dist_party_mc | 0 | 
                           state_dist, data = df_even_md.knowlow, weights = df_even_md.knowlow$weight)
mod3_biv_knowhigh <- felm(approval_rep ~ inc_tercile_ideo2 + nonwhite_mc + mc_seniority  | cong + dist_party_mc | 0 | 
                            state_dist, data = df_even_md.knowhigh, weights = df_even_md.knowhigh$weight)
# Female R respondents 
mod4_biv_knowlow <- felm(approval_rep ~ inc_tercile_ideo2 + nonwhite_mc + mc_seniority  | cong + dist_party_mc | 0 | 
                           state_dist, data = df_even_fr.knowlow, weights = df_even_fr.knowlow$weight)
mod4_biv_knowhigh <- felm(approval_rep ~ inc_tercile_ideo2 + nonwhite_mc + mc_seniority  | cong + dist_party_mc | 0 | 
                            state_dist, data = df_even_fr.knowhigh, weights = df_even_fr.knowhigh$weight)
# Male R respondents 
mod5_biv_knowlow <- felm(approval_rep ~ inc_tercile_ideo2 + nonwhite_mc + mc_seniority  | cong + dist_party_mc | 0 | 
                           state_dist, data = df_even_mr.knowlow, weights = df_even_mr.knowlow$weight)
mod5_biv_knowhigh <- felm(approval_rep ~ inc_tercile_ideo2 + nonwhite_mc + mc_seniority  | cong + dist_party_mc | 0 | 
                            state_dist, data = df_even_mr.knowhigh, weights = df_even_mr.knowhigh$weight)

## RACE 
# poc D respondents 
mod2_biv_knowlow.nw <- felm(approval_rep ~ inc_tercile_ideo2 + mc_gender + mc_seniority | cong + dist_party_mc | 0 | 
                              state_dist, data = df_even_nwd.knowlow, weights = df_even_nwd.knowlow$weight)
mod2_biv_knowhigh.nw <- felm(approval_rep ~ inc_tercile_ideo2 + mc_gender + mc_seniority | cong + dist_party_mc | 0 | 
                               state_dist, data = df_even_nwd.knowhigh, weights = df_even_nwd.knowhigh$weight)
# white D respondents 
mod3_biv_knowlow.nw <- felm(approval_rep ~ inc_tercile_ideo2 + mc_gender + mc_seniority | cong + dist_party_mc | 0 | 
                              state_dist, data = df_even_wd.knowlow, weights = df_even_wd.knowlow$weight)
mod3_biv_knowhigh.nw <- felm(approval_rep ~ inc_tercile_ideo2 + mc_gender + mc_seniority | cong + dist_party_mc | 0 | 
                               state_dist, data = df_even_wd.knowhigh, weights = df_even_wd.knowhigh$weight)
# poc R respondents 
mod4_biv_knowlow.nw <- felm(approval_rep ~ inc_tercile_ideo2 + mc_gender + mc_seniority | cong + dist_party_mc | 0 | 
                              state_dist, data = df_even_nwr.knowlow, weights = df_even_nwr.knowlow$weight)
mod4_biv_knowhigh.nw <- felm(approval_rep ~ inc_tercile_ideo2 + mc_gender + mc_seniority | cong + dist_party_mc | 0 | 
                               state_dist, data = df_even_nwr.knowhigh, weights = df_even_nwr.knowhigh$weight)
# white R respondents 
mod5_biv_knowlow.nw <- felm(approval_rep ~ inc_tercile_ideo2 + mc_gender + mc_seniority | cong + dist_party_mc | 0 | 
                              state_dist, data = df_even_wr.knowlow, weights = df_even_wr.knowlow$weight)
mod5_biv_knowhigh.nw <- felm(approval_rep ~ inc_tercile_ideo2 + mc_gender + mc_seniority | cong + dist_party_mc | 0 | 
                               state_dist, data = df_even_wr.knowhigh, weights = df_even_wr.knowhigh$weight)

# GENERATE FIGURE 
# Gender first 
# Low-knowledge 
mod_gen_ideo.lk.fd <- broom::tidy(mod2_biv_knowlow) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "Low Knowledge")
mod_gen_ideo.lk.md <- broom::tidy(mod3_biv_knowlow) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "Low Knowledge")
mod_gen_ideo.lk.fr <- broom::tidy(mod4_biv_knowlow) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "Low Knowledge")
mod_gen_ideo.lk.mr <- broom::tidy(mod5_biv_knowlow) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "Low Knowledge")
# High-knowledge 
mod_gen_ideo.hk.fd <- broom::tidy(mod2_biv_knowhigh) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "High Knowledge")
mod_gen_ideo.hk.md <- broom::tidy(mod3_biv_knowhigh) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "High Knowledge")
mod_gen_ideo.hk.fr <- broom::tidy(mod4_biv_knowhigh) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "High Knowledge")
mod_gen_ideo.hk.mr <- broom::tidy(mod5_biv_knowhigh) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "High Knowledge")

# Race Second 
# Low-knowledge 
mod_gen_ideo.lk.nwd.nw <- broom::tidy(mod2_biv_knowlow.nw) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "Low Knowledge")
mod_gen_ideo.lk.wd.nw <- broom::tidy(mod3_biv_knowlow.nw) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "Low Knowledge")
mod_gen_ideo.lk.nwr.nw <- broom::tidy(mod4_biv_knowlow.nw) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "Low Knowledge")
mod_gen_ideo.lk.wr.nw <- broom::tidy(mod5_biv_knowlow.nw) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "Low Knowledge")
# High-knowledge 
mod_gen_ideo.hk.nwd.nw <- broom::tidy(mod2_biv_knowhigh.nw) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "High Knowledge")
mod_gen_ideo.hk.wd.nw <- broom::tidy(mod3_biv_knowhigh.nw) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "High Knowledge")
mod_gen_ideo.hk.nwr.nw <- broom::tidy(mod4_biv_knowhigh.nw) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "High Knowledge")
mod_gen_ideo.hk.wr.nw <- broom::tidy(mod5_biv_knowhigh.nw) %>% filter(term%in%c("inc_tercile_ideo23")) %>% mutate(model = "High Knowledge")


# Create tibble with all regression output for Democratic women
term <- c("", "", "", "")

# gender 
models.lk.fd <- rbind(mod_gen_ideo.lk.fd) %>% select(-term)
models.lk.fd <- cbind(models.lk.fd, term) %>%
  mutate(identity = "Women Respondents", 
         party = "Democratic Respondents")
models.lk.md <- rbind(mod_gen_ideo.lk.md) %>% select(-term)
models.lk.md <- cbind(models.lk.md, term) %>%
  mutate(identity = "Men Respondents", 
         party = "Democratic Respondents")
models.lk.fr <- rbind(mod_gen_ideo.lk.fr) %>% select(-term)
models.lk.fr <- cbind(models.lk.fr, term) %>%
  mutate(identity = "Women Respondents", 
         party = "Republican Respondents")
models.lk.mr <- rbind(mod_gen_ideo.lk.mr) %>% select(-term)
models.lk.mr <- cbind(models.lk.mr, term) %>%
  mutate(identity = "Men Respondents", 
         party = "Republican Respondents")

models.hk.fd <- rbind(mod_gen_ideo.hk.fd) %>% select(-term)
models.hk.fd <- cbind(models.hk.fd, term) %>%
  mutate(identity = "Women Respondents", 
         party = "Democratic Respondents")
models.hk.md <- rbind(mod_gen_ideo.hk.md) %>% select(-term)
models.hk.md <- cbind(models.hk.md, term) %>%
  mutate(identity = "Men Respondents", 
         party = "Democratic Respondents")
models.hk.fr <- rbind(mod_gen_ideo.hk.fr) %>% select(-term)
models.hk.fr <- cbind(models.hk.fr, term) %>%
  mutate(identity = "Women Respondents", 
         party = "Republican Respondents")
models.hk.mr <- rbind(mod_gen_ideo.hk.mr) %>% select(-term)
models.hk.mr <- cbind(models.hk.mr, term) %>%
  mutate(identity = "Men Respondents", 
         party = "Republican Respondents")

# Race
models.lk.nwd <- rbind(mod_gen_ideo.lk.nwd.nw) %>% select(-term)
models.lk.nwd <- cbind(models.lk.nwd, term) %>%
  mutate(identity = "POC Respondents", 
         party = "Democratic Respondents")
models.lk.wd <- rbind(mod_gen_ideo.lk.wd.nw) %>% select(-term)
models.lk.wd <- cbind(models.lk.wd, term) %>%
  mutate(identity = "White Respondents", 
         party = "Democratic Respondents")
models.lk.nwr <- rbind(mod_gen_ideo.lk.nwr.nw) %>% select(-term)
models.lk.nwr <- cbind(models.lk.nwr, term) %>%
  mutate(identity = "POC Respondents", 
         party = "Republican Respondents")
models.lk.wr <- rbind(mod_gen_ideo.lk.wr.nw) %>% select(-term)
models.lk.wr <- cbind(models.lk.wr, term) %>%
  mutate(identity = "White Respondents", 
         party = "Republican Respondents")

models.hk.nwd <- rbind(mod_gen_ideo.hk.nwd.nw) %>% select(-term)
models.hk.nwd <- cbind(models.hk.nwd, term) %>%
  mutate(identity = "POC Respondents", 
         party = "Democratic Respondents")
models.hk.wd <- rbind(mod_gen_ideo.hk.wd.nw) %>% select(-term)
models.hk.wd <- cbind(models.hk.wd, term) %>%
  mutate(identity = "White Respondents", 
         party = "Democratic Respondents")
models.hk.nwr <- rbind(mod_gen_ideo.hk.nwr.nw) %>% select(-term)
models.hk.nwr <- cbind(models.hk.nwr, term) %>%
  mutate(identity = "POC Respondents", 
         party = "Republican Respondents")
models.hk.wr <- rbind(mod_gen_ideo.hk.wr.nw) %>% select(-term)
models.hk.wr <- cbind(models.hk.wr, term) %>%
  mutate(identity = "White Respondents", 
         party = "Republican Respondents")


models.knowledge <- rbind(models.lk.fd, models.lk.md, models.lk.fr, models.lk.mr,
                          models.hk.fd, models.hk.md, models.hk.fr, models.hk.mr,
                          models.lk.nwd, models.lk.wd, models.lk.nwr, models.lk.wr,
                          models.hk.nwd, models.hk.wd, models.hk.nwr, models.hk.wr)

models.knowledge$model <- factor(models.knowledge$model, 
                                 levels = c("Low Knowledge", 
                                            "High Knowledge"))

models.knowledge$identity <- factor(models.knowledge$identity, 
                                    levels = c("Women Respondents", "Men Respondents",
                                               "POC Respondents", "White Respondents"))

dwplot(models.knowledge,
       vline = geom_vline(
         xintercept = 0,
         colour = "grey60",
         linetype = 2
       ),
       dot_args = list(aes(shape = model), size=2),
       whisker_args = list(aes(color = model))) +
  theme_bw() + ylab("") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
        axis.text.y = element_text(size = 8),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10))+
  scale_colour_manual(values = c("grey68", "gray35", "black")) +
  scale_y_discrete(limits=rev) +
  xlab("Effects of MC Ideological Incongruence on Approval") +
  coord_flip() +
  facet_grid(party ~ identity)+
  guides(shape = guide_legend("model"), 
         colour = guide_legend("model")) +
  ylab("Level of Knowledge")

#
## K. Substitutes - Democratic ideologoical congruence with in-and out- party MCs - JUST PERCEIVED ----

# generate new datasets 
df_even_fd.d <- df_even_fd %>% filter(mc_party==1) # women D w D MC
df_even_md.d <- df_even_md %>% filter(mc_party==1) # men D w D MC
df_even_fd.r <- df_even_fd %>% filter(mc_party==0) # women D w R MC
df_even_md.r <- df_even_md %>% filter(mc_party==0) # men D w R MC

df_even_nwd.d <- df_even_nwd %>% filter(mc_party==1) # poc D w D MC
df_even_wd.d <- df_even_wd %>% filter(mc_party==1) # white D w D MC
df_even_nwd.r <- df_even_nwd %>% filter(mc_party==0) # poc D w R MC
df_even_wd.r <- df_even_wd %>% filter(mc_party==0) # white D w R MC

## Perceived ideology for gender 
mod2_p.ideo <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + state_dist | 0 | state_dist, data = df_even_fd.d, weights = df_even_fd.d$weight)
mod3_p.ideo <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + state_dist | 0 | state_dist, data = df_even_md.d, weights = df_even_md.d$weight)
mod4_p.ideo <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + state_dist | 0 | state_dist, data = df_even_fd.r, weights = df_even_fd.r$weight)
mod5_p.ideo <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + state_dist | 0 | state_dist, data = df_even_md.r, weights = df_even_md.r$weight)

## Perceived ideology for race 
mod2_p.ideo.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + state_dist | 0 | state_dist, data = df_even_nwd.d, weights = df_even_nwd.d$weight)
mod3_p.ideo.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + state_dist | 0 | state_dist, data = df_even_wd.d, weights = df_even_wd.d$weight)
mod4_p.ideo.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + state_dist | 0 | state_dist, data = df_even_nwd.r, weights = df_even_nwd.r$weight)
mod5_p.ideo.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + state_dist | 0 | state_dist, data = df_even_wd.r, weights = df_even_wd.r$weight)

stargazer(mod2_p.ideo,mod4_p.ideo,mod3_p.ideo,mod5_p.ideo,
          mod2_p.ideo.nw,mod4_p.ideo.nw,mod3_p.ideo.nw,mod5_p.ideo.nw,
          omit = c('[i][n][d]'),
          type='latex', header=FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = TRUE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{D Women \\\\ Respondents}", "\\shortstack{D Men \\\\ Respondents}",
                            "\\shortstack{D POC \\\\ Respondents}", "\\shortstack{D White \\\\ Respondents}"),
          column.separate = c(2,2,2, 2),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('MC Party', 'D','R','D','R','D','R','D','R'),
                         c('Congress FE', 'X','X','X','X','X','X','X','X'),
                         c('Dist FE', 'X','X','X','X','X','X','X','X')),
          title = "Effects of Gender/Racial Congruence and Ideological Congruence on MC Approval Ratings",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:gender.ideo.dem",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)

#
## L. Jackie Robinson Effect ----
# Models with Controls for Legislative Effectiveness ----

#GENDER 
# Female D respondents 
mod2_eff <- felm(approval_rep ~ gender_congruence + mc_seniority + nonwhite_mc + mc_effectiveness | cong + dist_party_mc | 0 | 
                   state_dist, data = df_even_fd, weights = df_even_fd$weight)
# Male D respondents 
mod3_eff <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc + mc_effectiveness | cong + dist_party_mc | 0 | 
                   state_dist, data = df_even_md, weights = df_even_md$weight)
# Female R respondents 
mod4_eff <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc + mc_effectiveness | cong + dist_party_mc | 0 | 
                   state_dist, data = df_even_fr, weights = df_even_fr$weight)
# Male R respondents 
mod5_eff <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc + mc_effectiveness | cong + dist_party_mc | 0 | 
                   state_dist, data = df_even_mr, weights = df_even_mr$weight)

## DW-NOMINATE ideology for gender 
mod2_ideo_eff <- felm(approval_rep ~ inc_tercile_ideo1 + gender_congruence:inc_tercile_ideo1 + mc_seniority + nonwhite_mc + mc_effectiveness | cong + dist_party_mc | 0 | state_dist, data = df_even_fd, weights = df_even_fd$weight)

mod3_ideo_eff <- felm(approval_rep ~ inc_tercile_ideo1 + gender_congruence:inc_tercile_ideo1 + mc_seniority + nonwhite_mc + mc_effectiveness | cong + dist_party_mc | 0 | state_dist, data = df_even_md, weights = df_even_md$weight)

mod4_ideo_eff <- felm(approval_rep ~ inc_tercile_ideo1 + gender_congruence:inc_tercile_ideo1 + mc_seniority + nonwhite_mc + mc_effectiveness | cong + dist_party_mc | 0 | state_dist, data = df_even_fr, weights = df_even_fr$weight)

mod5_ideo_eff <- felm(approval_rep ~ inc_tercile_ideo1 + gender_congruence:inc_tercile_ideo1 + mc_seniority + nonwhite_mc + mc_effectiveness | cong + dist_party_mc | 0 | state_dist, data = df_even_mr, weights = df_even_mr$weight)

## Perceived ideology for gender 
mod2_p.ideo_eff <- felm(approval_rep ~ inc_tercile_ideo2 + gender_congruence:inc_tercile_ideo2 + mc_seniority + nonwhite_mc + mc_effectiveness | cong + dist_party_mc | 0 | state_dist, data = df_even_fd, weights = df_even_fd$weight)

mod3_p.ideo_eff <- felm(approval_rep ~ inc_tercile_ideo2 + gender_congruence:inc_tercile_ideo2 + mc_seniority + nonwhite_mc + mc_effectiveness | cong + dist_party_mc | 0 | state_dist, data = df_even_md, weights = df_even_md$weight)

mod4_p.ideo_eff <- felm(approval_rep ~ inc_tercile_ideo2 + gender_congruence:inc_tercile_ideo2 + mc_seniority + nonwhite_mc + mc_effectiveness | cong + dist_party_mc | 0 | state_dist, data = df_even_fr, weights = df_even_fr$weight)

mod5_p.ideo_eff <- felm(approval_rep ~ inc_tercile_ideo2 + gender_congruence:inc_tercile_ideo2 + mc_seniority + nonwhite_mc + mc_effectiveness | cong + dist_party_mc | 0 | state_dist, data = df_even_mr, weights = df_even_mr$weight)

# Calculate number of treated districts in each dataset
treat2_eff <- df_even_fd %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)&!is.na(mc_effectiveness)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod2_t_eff <- length(which(treat2_eff$treated==1))

treat3_eff <- df_even_md %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)&!is.na(mc_effectiveness)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod3_t_eff <- length(which(treat3_eff$treated==1))

treat4_eff <- df_even_fr %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)&!is.na(mc_effectiveness)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod4_t_eff <- length(which(treat4_eff$treated==1))

treat5_eff <- df_even_mr %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(mc_gender)&!is.na(mc_effectiveness)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod5_t_eff <- length(which(treat5_eff$treated==1))


#RACE 
# Nonwhite D respondents 
mod2.nw_eff <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender + mc_effectiveness| cong + dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
# White D respondents 
mod3.nw_eff <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender + mc_effectiveness| cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)
# Nonwhite R respondents 
mod4.nw_eff <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender + mc_effectiveness| cong + dist_party_mc | 0 | state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
# White R respondents 
mod5.nw_eff <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender + mc_effectiveness| cong + dist_party_mc | 0 | state_dist, data = df_even_wr, weights = df_even_wr$weight)

## DW-NOMINATE ideology for nonwhite 
mod2_ideo.nw_eff <- felm(approval_rep ~ inc_tercile_ideo1 + race_congruence:inc_tercile_ideo1 + mc_seniority + mc_gender  + mc_effectiveness| cong + dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)

mod3_ideo.nw_eff <- felm(approval_rep ~ inc_tercile_ideo1 + race_congruence:inc_tercile_ideo1 + mc_seniority + mc_gender  + mc_effectiveness| cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)

mod4_ideo.nw_eff <- felm(approval_rep ~ inc_tercile_ideo1 + race_congruence:inc_tercile_ideo1 + mc_seniority + mc_gender  + mc_effectiveness| cong + dist_party_mc | 0 | state_dist, data = df_even_nwr, weights = df_even_nwr$weight)

mod5_ideo.nw_eff <- felm(approval_rep ~ inc_tercile_ideo1 + race_congruence:inc_tercile_ideo1 + mc_seniority + mc_gender  + mc_effectiveness| cong + dist_party_mc | 0 | state_dist, data = df_even_wr, weights = df_even_wr$weight)


## Perceived ideology for nonwhite 
mod2_p.ideo.nw_eff <- felm(approval_rep ~ inc_tercile_ideo2 + race_congruence:inc_tercile_ideo2 + mc_seniority + mc_gender  + mc_effectiveness| cong + dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)

mod3_p.ideo.nw_eff <- felm(approval_rep ~ inc_tercile_ideo2 + race_congruence:inc_tercile_ideo2 + mc_seniority + mc_gender  + mc_effectiveness| cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)

mod4_p.ideo.nw_eff <- felm(approval_rep ~ inc_tercile_ideo2 + race_congruence:inc_tercile_ideo2 + mc_seniority + mc_gender  + mc_effectiveness| cong + dist_party_mc | 0 | state_dist, data = df_even_nwr, weights = df_even_nwr$weight)

mod5_p.ideo.nw_eff <- felm(approval_rep ~ inc_tercile_ideo2 + race_congruence:inc_tercile_ideo2 + mc_seniority + mc_gender  + mc_effectiveness| cong + dist_party_mc | 0 | state_dist, data = df_even_wr, weights = df_even_wr$weight)

# Calculate number of treated districts in each dataset
treat2.nw_eff <- df_even_nwd %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)&!is.na(mc_effectiveness)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod2_t.nw_eff <- length(which(treat2.nw_eff$treated==1))

treat3.nw_eff <- df_even_wd %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)&!is.na(mc_effectiveness)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod3_t.nw_eff <- length(which(treat3.nw_eff$treated==1))

treat4.nw_eff <- df_even_nwr %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)&!is.na(mc_effectiveness)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod4_t.nw_eff <- length(which(treat4.nw_eff$treated==1))

treat5.nw_eff <- df_even_wr %>%
  filter(!is.na(state_dist)&!is.na(approval_rep)&!is.na(nonwhite_mc)&!is.na(mc_effectiveness)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod5_t.nw_eff <- length(which(treat5.nw_eff$treated==1))


# Table together 

stargazer(mod2_eff, mod3_eff, mod4_eff, mod5_eff,
          mod2.nw_eff, mod3.nw_eff, mod4.nw_eff, mod5.nw_eff,
          omit = '[i][n][d]', type='latex', header=FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{D Women \\\\ Respondents}", "\\shortstack{D Men \\\\ Respondents}", 
                            "\\shortstack{R Women \\\\ Respondents}", "\\shortstack{R Men \\\\ Respondents}",
                            "\\shortstack{D POC \\\\ Respondents}", "\\shortstack{D White \\\\ Respondents}",
                            "\\shortstack{R POC \\\\ Respondents}", "\\shortstack{R White \\\\ Respondents}"),
          #covariate.labels=c("Gender Congruence = 1", "MC Seniority", "POC MC", "MC Effectiveness"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X','X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X','X','X','X','X'),
                         c("N districts w MC identity change", mod2_t_eff, mod3_t_eff, mod4_t_eff, mod5_t_eff,
                           mod2_t.nw_eff, mod3_t.nw_eff, mod4_t.nw_eff, mod5_t.nw_eff)),
          title = "Effects of Identity Congruence on MC Approval Ratings Controlling for Effectiveness",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:gender",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001"), notes.append = F)


# Effectiveness and approval over time ----

# Female D respondents 
mod1_eff.yr <- felm(approval_rep ~ factor(year) + mc_seniority + nonwhite_mc + mc_effectiveness + gender_congruence:factor(year)
                    | dist_party_mc | 0 | state_dist, data = df_even_fd, weights = df_even_fd$weight)
# Male D respondents 
mod2_eff.yr <- felm(approval_rep ~ factor(year) + mc_seniority + nonwhite_mc + mc_effectiveness + gender_congruence:factor(year)
                    | dist_party_mc | 0 | state_dist, data = df_even_md, weights = df_even_md$weight)
# Female R respondents 
mod3_eff.yr <- felm(approval_rep ~ factor(year) + mc_seniority + nonwhite_mc + mc_effectiveness + gender_congruence:factor(year)
                    | dist_party_mc | 0 | state_dist, data = df_even_fr, weights = df_even_fr$weight)
# Male R respondents 
mod4_eff.yr <- felm(approval_rep ~ factor(year) + mc_seniority + nonwhite_mc + mc_effectiveness + gender_congruence:factor(year)
                    | dist_party_mc | 0 | state_dist, data = df_even_mr, weights = df_even_mr$weight)


# Nonwhite D respondents 
mod1.nw_eff.yr <- felm(approval_rep ~ factor(year) + mc_seniority + mc_gender + mc_effectiveness + race_congruence:factor(year)
                       | dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
# White D respondents 
mod2.nw_eff.yr <- felm(approval_rep ~ factor(year) + mc_seniority + mc_gender + mc_effectiveness + race_congruence:factor(year)
                       | dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)
# Nonwhite R respondents 
mod3.nw_eff.yr <- felm(approval_rep ~ factor(year) + mc_seniority + mc_gender + mc_effectiveness + race_congruence:factor(year)
                       | dist_party_mc | 0 | state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
# White R respondents 
mod4.nw_eff.yr <- felm(approval_rep ~ factor(year) + mc_seniority + mc_gender + mc_effectiveness + race_congruence:factor(year)
                       | dist_party_mc | 0 | state_dist, data = df_even_wr, weights = df_even_wr$weight)

# create data frames with regression output of interest
mod1.eff_df <- broom::tidy(mod1_eff.yr) %>% 
  filter(term%in%c("factor(year)2008:gender_congruence",
                   "factor(year)2010:gender_congruence",
                   "factor(year)2012:gender_congruence",
                   "factor(year)2014:gender_congruence",
                   "factor(year)2016:gender_congruence",
                   "factor(year)2018:gender_congruence",
                   "factor(year)2020:gender_congruence")) %>% 
  mutate(model = "Women",
         term = case_when(term=="factor(year)2008:gender_congruence"~"2008",
                          term=="factor(year)2010:gender_congruence"~"2010",
                          term=="factor(year)2012:gender_congruence"~"2012",
                          term=="factor(year)2014:gender_congruence"~"2014",
                          term=="factor(year)2016:gender_congruence"~"2016",
                          term=="factor(year)2018:gender_congruence"~"2018",
                          term=="factor(year)2020:gender_congruence"~"2020"))

mod2.eff_df <- broom::tidy(mod2_eff.yr) %>% 
  filter(term%in%c("factor(year)2008:gender_congruence",
                   "factor(year)2010:gender_congruence",
                   "factor(year)2012:gender_congruence",
                   "factor(year)2014:gender_congruence",
                   "factor(year)2016:gender_congruence",
                   "factor(year)2018:gender_congruence",
                   "factor(year)2020:gender_congruence")) %>% 
  mutate(model = "Men",
         term = case_when(term=="factor(year)2008:gender_congruence"~"2008",
                          term=="factor(year)2010:gender_congruence"~"2010",
                          term=="factor(year)2012:gender_congruence"~"2012",
                          term=="factor(year)2014:gender_congruence"~"2014",
                          term=="factor(year)2016:gender_congruence"~"2016",
                          term=="factor(year)2018:gender_congruence"~"2018",
                          term=="factor(year)2020:gender_congruence"~"2020"))

mod3.eff_df <- broom::tidy(mod3_eff.yr) %>% 
  filter(term%in%c("factor(year)2008:gender_congruence",
                   "factor(year)2010:gender_congruence",
                   "factor(year)2012:gender_congruence",
                   "factor(year)2014:gender_congruence",
                   "factor(year)2016:gender_congruence",
                   "factor(year)2018:gender_congruence",
                   "factor(year)2020:gender_congruence")) %>% 
  mutate(model = "Women",
         term = case_when(term=="factor(year)2008:gender_congruence"~"2008",
                          term=="factor(year)2010:gender_congruence"~"2010",
                          term=="factor(year)2012:gender_congruence"~"2012",
                          term=="factor(year)2014:gender_congruence"~"2014",
                          term=="factor(year)2016:gender_congruence"~"2016",
                          term=="factor(year)2018:gender_congruence"~"2018",
                          term=="factor(year)2020:gender_congruence"~"2020"))

mod4.eff_df <- broom::tidy(mod4_eff.yr) %>% 
  filter(term%in%c("factor(year)2008:gender_congruence",
                   "factor(year)2010:gender_congruence",
                   "factor(year)2012:gender_congruence",
                   "factor(year)2014:gender_congruence",
                   "factor(year)2016:gender_congruence",
                   "factor(year)2018:gender_congruence",
                   "factor(year)2020:gender_congruence")) %>% 
  mutate(model = "Men",
         term = case_when(term=="factor(year)2008:gender_congruence"~"2008",
                          term=="factor(year)2010:gender_congruence"~"2010",
                          term=="factor(year)2012:gender_congruence"~"2012",
                          term=="factor(year)2014:gender_congruence"~"2014",
                          term=="factor(year)2016:gender_congruence"~"2016",
                          term=="factor(year)2018:gender_congruence"~"2018",
                          term=="factor(year)2020:gender_congruence"~"2020"))

# create tibble with all regression output
models.eff <- rbind(mod1.eff_df, mod2.eff_df)
models2.eff <- rbind(mod3.eff_df, mod4.eff_df)
models.eff <- models.eff %>% mutate(party = "Democrats")
models2.eff <- models2.eff %>% mutate(party = "Republicans")

both.models.gender.eff <- rbind(models.eff, models2.eff)

# race 
mod1.eff_df.nw <- broom::tidy(mod1.nw_eff.yr) %>% 
  filter(term%in%c("factor(year)2008:race_congruence",
                   "factor(year)2010:race_congruence",
                   "factor(year)2012:race_congruence",
                   "factor(year)2014:race_congruence",
                   "factor(year)2016:race_congruence",
                   "factor(year)2018:race_congruence",
                   "factor(year)2020:race_congruence")) %>% 
  mutate(model = "POC",
         term = case_when(term=="factor(year)2008:race_congruence"~"2008",
                          term=="factor(year)2010:race_congruence"~"2010",
                          term=="factor(year)2012:race_congruence"~"2012",
                          term=="factor(year)2014:race_congruence"~"2014",
                          term=="factor(year)2016:race_congruence"~"2016",
                          term=="factor(year)2018:race_congruence"~"2018",
                          term=="factor(year)2020:race_congruence"~"2020"))

mod2.eff_df.nw <- broom::tidy(mod2.nw_eff.yr) %>% 
  filter(term%in%c("factor(year)2008:race_congruence",
                   "factor(year)2010:race_congruence",
                   "factor(year)2012:race_congruence",
                   "factor(year)2014:race_congruence",
                   "factor(year)2016:race_congruence",
                   "factor(year)2018:race_congruence",
                   "factor(year)2020:race_congruence")) %>% 
  mutate(model = "White",
         term = case_when(term=="factor(year)2008:race_congruence"~"2008",
                          term=="factor(year)2010:race_congruence"~"2010",
                          term=="factor(year)2012:race_congruence"~"2012",
                          term=="factor(year)2014:race_congruence"~"2014",
                          term=="factor(year)2016:race_congruence"~"2016",
                          term=="factor(year)2018:race_congruence"~"2018",
                          term=="factor(year)2020:race_congruence"~"2020"))

mod3.eff_df.nw <- broom::tidy(mod3.nw_eff.yr) %>% 
  filter(term%in%c("factor(year)2008:race_congruence",
                   "factor(year)2010:race_congruence",
                   "factor(year)2012:race_congruence",
                   "factor(year)2014:race_congruence",
                   "factor(year)2016:race_congruence",
                   "factor(year)2018:race_congruence",
                   "factor(year)2020:race_congruence")) %>% 
  mutate(model = "POC",
         term = case_when(term=="factor(year)2008:race_congruence"~"2008",
                          term=="factor(year)2010:race_congruence"~"2010",
                          term=="factor(year)2012:race_congruence"~"2012",
                          term=="factor(year)2014:race_congruence"~"2014",
                          term=="factor(year)2016:race_congruence"~"2016",
                          term=="factor(year)2018:race_congruence"~"2018",
                          term=="factor(year)2020:race_congruence"~"2020"))

mod4.eff_df.nw <- broom::tidy(mod4.nw_eff.yr) %>% 
  filter(term%in%c("factor(year)2008:race_congruence",
                   "factor(year)2010:race_congruence",
                   "factor(year)2012:race_congruence",
                   "factor(year)2014:race_congruence",
                   "factor(year)2016:race_congruence",
                   "factor(year)2018:race_congruence",
                   "factor(year)2020:race_congruence")) %>% 
  mutate(model = "White",
         term = case_when(term=="factor(year)2008:race_congruence"~"2008",
                          term=="factor(year)2010:race_congruence"~"2010",
                          term=="factor(year)2012:race_congruence"~"2012",
                          term=="factor(year)2014:race_congruence"~"2014",
                          term=="factor(year)2016:race_congruence"~"2016",
                          term=="factor(year)2018:race_congruence"~"2018",
                          term=="factor(year)2020:race_congruence"~"2020"))

# create tibble with all regression output
models.nw.eff <- rbind(mod1.eff_df.nw, mod2.eff_df.nw)
models2.nw.eff <- rbind(mod3.eff_df.nw, mod4.eff_df.nw)
models.nw.eff <- models.nw.eff %>% mutate(party = "Democrats")
models2.nw.eff <- models2.nw.eff %>% mutate(party = "Republicans")

both.models.race.eff <- rbind(models.nw.eff, models2.nw.eff)

# combine with gender models after adding MC Gender and MC Race
both.models.gender.eff <- both.models.gender.eff %>% mutate(identity = "Effect of Gender Congruence")
both.models.race.eff <- both.models.race.eff %>% mutate(identity = "Effect of Race Congruence")

yearly.models.eff <- rbind(both.models.gender.eff, both.models.race.eff)

yearly.models.eff$identity <- factor(yearly.models.eff$identity, 
                                     levels = c("Effect of Gender Congruence", "Effect of Race Congruence"))

dwplot(yearly.models.eff,
       vline = geom_vline(
         xintercept = 0,
         colour = "grey60",
         linetype = 2
       ),
       dot_args = list(aes(shape = model), size=2.5),
       whisker_args = list(aes(color = model))) +
  theme_bw() + ylab("") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
        axis.text.y = element_text(size = 8),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10))+
  scale_colour_manual(values = c("darkgrey", "black", "darkgrey", "black")) +
  scale_y_discrete(limits=rev) +
  xlab("Marginal Effect of MC Identity Congruence on Approval") +
  ylab("Year") +
  coord_flip() +
  guides(shape = guide_legend("model"), 
         colour = guide_legend("model")) +
  facet_grid(identity ~ party)

# Effectiveness averages over time ----

# generate average effectiveness in each year for white, poc, men, women
# just among Democratic MCs 
average.eff.gender <- df_even2 %>%
  select(mc_effectiveness, mc_gender, year, mc_party) %>%
  filter(mc_party==1) %>%
  group_by(mc_gender, year) %>%
  summarise(avg.effect = mean(mc_effectiveness, na.rm = T)) %>%
  rename(model = mc_gender) %>%
  mutate(model = case_when(model==0 ~ "Man",
                           model==1 ~ "Woman")) %>%
  filter(!is.na(model))

average.eff.race <- df_even2 %>%
  select(mc_effectiveness, nonwhite_mc, year, mc_party) %>%
  filter(mc_party==1) %>%
  group_by(nonwhite_mc, year) %>%
  summarise(avg.effect = mean(mc_effectiveness, na.rm = T)) %>%
  rename(model = nonwhite_mc) %>%
  mutate(model = case_when(model==0 ~ "White",
                           model==1 ~ "POC")) %>%
  filter(!is.na(model))

average.eff.both.d <- rbind(average.eff.gender,average.eff.race) %>%
  mutate(party="Democratic MCs")


# Republicans 
average.eff.gender <- df_even2 %>%
  select(mc_effectiveness, mc_gender, year, mc_party) %>%
  filter(mc_party==0) %>%
  group_by(mc_gender, year) %>%
  summarise(avg.effect = mean(mc_effectiveness, na.rm = T)) %>%
  rename(model = mc_gender) %>%
  mutate(model = case_when(model==0 ~ "Man",
                           model==1 ~ "Woman")) %>%
  filter(!is.na(model))

average.eff.race <- df_even2 %>%
  select(mc_effectiveness, nonwhite_mc, year, mc_party) %>%
  filter(mc_party==0) %>%
  group_by(nonwhite_mc, year) %>%
  summarise(avg.effect = mean(mc_effectiveness, na.rm = T)) %>%
  rename(model = nonwhite_mc) %>%
  mutate(model = case_when(model==0 ~ "White",
                           model==1 ~ "POC")) %>%
  filter(!is.na(model))

average.eff.both.r <- rbind(average.eff.gender,average.eff.race) %>%
  mutate(party="Republican MCs")

# plot both parties 
full.avg <- rbind(average.eff.both.d, average.eff.both.r)

ggplot(full.avg, aes(year, avg.effect, linetype = model)) +
  geom_point() + geom_line() +
  scale_linetype_discrete(name = "") + 
  theme_bw() +
  scale_x_continuous(breaks = c(2008, 2010, 2012, 2014, 2016, 2018, 2020)) +
  theme(plot.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_rect(fill = "white"),
        legend.position = "bottom") +
  labs(x = "Years", y = "Average Effectiveness") +
  ylim(0,10) +
  facet_wrap(~ party)

#
## M. Misperceiving ideology ----
# Perceived Rates of Ideology 

# First checking if there's perception of more ideology congruence for identity congruence than identity incongruence 
# Average perceived ideology incongruence level for gender congruence 
wom_cong <- mean(na.omit(df_even2$incongruence_ideo2[df_even2$female_congruence==1]))
man_cong <- mean(na.omit(df_even2$incongruence_ideo2[df_even2$male_congruence==1]))

# Average perceived ideology incongruence level for gender incongruence 
wom_inc <- mean(na.omit(df_even2$incongruence_ideo2[df_even2$female_congruence==0]))
man_inc <- mean(na.omit(df_even2$incongruence_ideo2[df_even2$male_congruence==0]))

# Average perceived ideology incongruence level for race congruence 
poc_cong <- mean(na.omit(df_even2$incongruence_ideo2[df_even2$nonwhite_and_racecong==1]))
white_cong <- mean(na.omit(df_even2$incongruence_ideo2[df_even2$white_congruence==1]))

# Average perceived ideology incongruence level for race incongruence 
poc_inc <- mean(na.omit(df_even2$incongruence_ideo2[df_even2$nonwhite_and_racecong==0]))
white_inc <- mean(na.omit(df_even2$incongruence_ideo2[df_even2$white_congruence==0]))

toprow <- cbind(wom_cong, man_cong, poc_cong, white_cong)
bottomrow <- cbind(wom_inc, man_inc, poc_inc, white_inc)
combo <- rbind(round(toprow,4), round(bottomrow, 4))

rownames(combo) <- c("Identity Congruence", "Identity Incongruence")
colnames(combo) <- c("Women", "Men", "POC", "White")

combo <- as.matrix(combo)

stargazer(combo, type = "latex", title="Average Perceived Ideological Incongruence by Gender/Race Congruence and Incongruence", 
          digits=3, header=F,style="apsr",
          colnames = TRUE, rownames = TRUE, 
          column.sep.width = ".5pt", font.size="scriptsize")

# Doesn't look like there is greater perceived ideological congruence for MCs who are gender or race congruent.

## DOING THIS OVER TIME 
library(estimatr)

#subset to only Dem MCs for fem D respondents 
df_even_fd_d <- df_even_fd%>%
  filter(mc_party==1)
#subset to only Rep MCs for fem D respondents 
df_even_fd_r <- df_even_fd%>%
  filter(mc_party==0)
#subset to only Dem MCs for male D respondents 
df_even_md_d <- df_even_md%>%
  filter(mc_party==1)
#subset to only Rep MCs for male D respondents 
df_even_md_r <- df_even_md%>%
  filter(mc_party==0)
#subset to only Dem MCs for fem R respondents 
df_even_fr_d <- df_even_fr%>%
  filter(mc_party==1)
#subset to only Rep MCs for fem R respondents 
df_even_fr_r <- df_even_fr%>%
  filter(mc_party==0)
#subset to only Dem MCs for male R respondents 
df_even_mr_d <- df_even_mr%>%
  filter(mc_party==1)
#subset to only Rep MCs for male R respondents 
df_even_mr_r <- df_even_mr%>%
  filter(mc_party==0)

inc.mean <- function(yr, dataset){
  dif.mean <- difference_in_means(incongruence_ideo2 ~ mc_gender, dataset, 
                                  subset = c(year==yr),
                                  weights = weight, condition1 = 0, condition2 = 1)
  estimate<- c(dif.mean$coefficients)
  std.error <- c(dif.mean$std.error)
  p.value <- c(dif.mean$p.value)
  statistic <- c(dif.mean$statistic)
  
  rbind(estimate,std.error,statistic,p.value)
}

yr <- c(2008,2010,2012, 2014, 2016, 2018, 2020)
# Democrat MCs for fem D
difmeans_d1 <- lapply(yr,df_even_fd_d, FUN=inc.mean)
# Democrat MCs for fem R
difmeans_d2 <- lapply(yr,df_even_fr_d, FUN=inc.mean)
# Democrat MCs for male D
difmeans_d3 <- lapply(yr,df_even_md_d, FUN=inc.mean)
# Democrat MCs for male R
difmeans_d4 <- lapply(yr,df_even_mr_d, FUN=inc.mean)
# Republican MCs for fem D
difmeans_r1 <- lapply(yr,df_even_fd_r, FUN=inc.mean)
# Republican MCs for fem R
difmeans_r2 <- lapply(yr,df_even_fr_r, FUN=inc.mean)
# Republican MCs for male D
difmeans_r3 <- lapply(yr,df_even_md_r, FUN=inc.mean)
# Republican MCs for male R
difmeans_r4 <- lapply(yr,df_even_mr_r, FUN=inc.mean)

# cleaning up with function 
clean.fcn <- function(frame){
  df_difmean<-t(as.data.frame(frame))
  rownames(df_difmean) <- c("1","2","3", "4", "5", "6", "7")
  term <- as.data.frame(yr) %>% rename(term = yr) %>% mutate(term=as.character(term))
  df_difmean <- cbind(term,df_difmean)
  
  df_difmean
}

# D women with D MCs
df_difmean_d1 <- clean.fcn(difmeans_d1)
df_difmean_d1$model <- "Democratic Women"
# R women with D MCs
df_difmean_d2 <- clean.fcn(difmeans_d2)
df_difmean_d2$model <- "Republican Women"
# D men with D MCs
df_difmean_d3 <- clean.fcn(difmeans_d3)
df_difmean_d3$model <- "Democratic Men"
# R men with D MCs
df_difmean_d4 <- clean.fcn(difmeans_d4)
df_difmean_d4$model <- "Republican Men"
# D women with D MCs
df_difmean_r1 <- clean.fcn(difmeans_r1)
df_difmean_r1$model <- "Democratic Women"
# R women with D MCs
df_difmean_r2 <- clean.fcn(difmeans_r2)
df_difmean_r2$model <- "Republican Women"
# D men with D MCs
df_difmean_r3 <- clean.fcn(difmeans_r3)
df_difmean_r3$model <- "Democratic Men"
# R men with D MCs
df_difmean_r4 <- clean.fcn(difmeans_r4)
df_difmean_r4$model <- "Republican Men"

# combing to plot 
df_difmean_R <- rbind(df_difmean_r1, df_difmean_r2, df_difmean_r3, df_difmean_r4)
df_difmean_D <- rbind(df_difmean_d1, df_difmean_d2, df_difmean_d3, df_difmean_d4)

df_difmean_R$party <- "Republican MCs"
df_difmean_D$party <- "Democratic MCs"

df_difmean_gender <- rbind(df_difmean_D, df_difmean_R)

difmean.plot <- dwplot(df_difmean_gender,
                       vline = geom_vline(
                         xintercept = 0,
                         colour = "grey60",
                         linetype = 2
                       ),
                       dot_args = list(aes(shape = model), size=2),
                       whisker_args = list(aes(color = model))) +
  theme_bw() + ylab("") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 10, face = "bold"),
        axis.title.x = element_text(size = 8),
        axis.title.y = element_text(size = 8),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10))+
  scale_colour_manual(values = c("lightgrey","grey","darkgrey","black")) + 
  scale_y_discrete(limits=rev) +
  ylab("Year") + #xlim(-.2,.1) + 
  xlab("Perceived Ideological Congruence Difference")+
  coord_flip() +
  guides(shape = guide_legend("model"), 
         colour = guide_legend("model")) +
  facet_wrap(~party)

difmean.plot

## DOING FOR RACE AS WELL 

inc.mean2 <- function(yr, dataset){
  dif.mean <- difference_in_means(incongruence_ideo2 ~ nonwhite_mc, dataset, 
                                  subset = c(year==yr),
                                  weights = weight, condition1 = 0, condition2 = 1)
  estimate<- c(dif.mean$coefficients)
  std.error <- c(dif.mean$std.error)
  p.value <- c(dif.mean$p.value)
  statistic <- c(dif.mean$statistic)
  
  rbind(estimate,std.error,statistic,p.value)
}

#subset to only Dem MCs for poc D respondents 
df_even_nwd_d <- df_even_nwd%>%
  filter(mc_party==1)
#subset to only Rep MCs for poc D respondents 
df_even_nwd_r <- df_even_nwd%>%
  filter(mc_party==0)
#subset to only Dem MCs for white D respondents 
df_even_wd_d <- df_even_wd%>%
  filter(mc_party==1)
#subset to only Rep MCs for white D respondents 
df_even_wd_r <- df_even_wd%>%
  filter(mc_party==0)
#subset to only Dem MCs for poc R respondents 
df_even_nwr_d <- df_even_nwr%>%
  filter(mc_party==1)
#subset to only Rep MCs for poc R respondents 
df_even_nwr_r <- df_even_nwr%>%
  filter(mc_party==0)
#subset to only Dem MCs for white R respondents 
df_even_wr_d <- df_even_wr%>%
  filter(mc_party==1)
#subset to only Rep MCs for white R respondents 
df_even_wr_r <- df_even_wr%>%
  filter(mc_party==0)

yr <- c(2008,2010,2012, 2014, 2016, 2018, 2020)
# Democrat MCs for poc D
difmeans_d1 <- lapply(yr,df_even_nwd_d, FUN=inc.mean2)
# Democrat MCs for poc R
difmeans_d2 <- lapply(yr,df_even_nwr_d, FUN=inc.mean2)
# Democrat MCs for white D
difmeans_d3 <- lapply(yr,df_even_wd_d, FUN=inc.mean2)
# Democrat MCs for white R
difmeans_d4 <- lapply(yr,df_even_wr_d, FUN=inc.mean2)
# Republican MCs for poc D
difmeans_r1 <- lapply(yr,df_even_nwd_r, FUN=inc.mean2)
# Republican MCs for poc R
difmeans_r2 <- lapply(yr,df_even_nwr_r, FUN=inc.mean2)
# Republican MCs for white D
difmeans_r3 <- lapply(yr,df_even_wd_r, FUN=inc.mean2)
# Republican MCs for white R
difmeans_r4 <- lapply(yr,df_even_wr_r, FUN=inc.mean2)

# cleaning up with function 
# D poc with D MCs
df_difmean_d1 <- clean.fcn(difmeans_d1)
df_difmean_d1$model <- "Democratic POC"
# R poc with D MCs
df_difmean_d2 <- clean.fcn(difmeans_d2)
df_difmean_d2$model <- "Republican POC"
# D whites with D MCs
df_difmean_d3 <- clean.fcn(difmeans_d3)
df_difmean_d3$model <- "Democratic Whites"
# R whites with D MCs
df_difmean_d4 <- clean.fcn(difmeans_d4)
df_difmean_d4$model <- "Republican Whites"
# D poc with D MCs
df_difmean_r1 <- clean.fcn(difmeans_r1)
df_difmean_r1$model <- "Democratic POC"
# R poc with D MCs
df_difmean_r2 <- clean.fcn(difmeans_r2)
df_difmean_r2$model <- "Republican POC"
# D whites with D MCs
df_difmean_r3 <- clean.fcn(difmeans_r3)
df_difmean_r3$model <- "Democratic Whites"
# R whites with D MCs
df_difmean_r4 <- clean.fcn(difmeans_r4)
df_difmean_r4$model <- "Republican Whites"

# combing to plot 
df_difmean_R <- rbind(df_difmean_r1, df_difmean_r2, df_difmean_r3, df_difmean_r4)
df_difmean_D <- rbind(df_difmean_d1, df_difmean_d2, df_difmean_d3, df_difmean_d4)

df_difmean_R$party <- "Republican MCs"
df_difmean_D$party <- "Democratic MCs"

df_difmean_race <- rbind(df_difmean_D, df_difmean_R)

difmean.plot <- dwplot(df_difmean_race,
                       vline = geom_vline(
                         xintercept = 0,
                         colour = "grey60",
                         linetype = 2
                       ),
                       dot_args = list(aes(shape = model), size=2),
                       whisker_args = list(aes(color = model))) +
  theme_bw() + ylab("") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 10, face = "bold"),
        axis.title.x = element_text(size = 8),
        axis.title.y = element_text(size = 8),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10))+
  scale_colour_manual(values = c("lightgrey","grey","darkgrey","black")) + 
  scale_y_discrete(limits=rev) +
  ylab("Year") + #xlim(-.2,.1) + 
  xlab("Perceived Ideological Congruence Difference")+
  coord_flip() +
  guides(shape = guide_legend("model"), 
         colour = guide_legend("model")) +
  facet_wrap(~party)

difmean.plot

## N. Racial Resentment and Hostile Sexism Full Tables ----

# interact Racial congruence and rr with district-level controls for White Dems 
mod_rr1_wd <- felm(approval_rep ~ nonwhite_mc + rr_index:nonwhite_mc + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data=df_even_wd, weights=df_even_wd$weight)
# interact Racial congruence and rr with district-level controls for POC Dems 
mod_rr2_nwd <- felm(approval_rep ~ nonwhite_mc + rr_index:nonwhite_mc + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data=df_even_nwd, weights=df_even_nwd$weight)
# interact Racial congruence and rr with district-level controls for White GOP 
mod_rr3_wr <- felm(approval_rep ~ nonwhite_mc + rr_index:nonwhite_mc + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data=df_even_wr, weights=df_even_wr$weight)
# interact Racial congruence and rr with district-level controls for POC GOP 
mod_rr4_nwr <- felm(approval_rep ~ nonwhite_mc + rr_index:nonwhite_mc + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data=df_even_nwr, weights=df_even_nwr$weight)


# table
stargazer(mod_rr1_wd, mod_rr2_nwd, mod_rr3_wr, mod_rr4_nwr, type="latex", 
          column.labels=c("\\shortstack{Democrat \\\\ White}", "\\shortstack{Democrat \\\\ POC}", "\\shortstack{Republican \\\\ White}", "\\shortstack{Republican \\\\ POC}"), 
          model.numbers=F, style="apsr",
          header=F, title = "Effects of Racial Congruence on MC Approval among D and R Respondents, Interacting MC Race with Racial Resentment",
          covariate.labels=c("POC MC = 1","MC Gender","MC Seniority", "RR | POC MC = 1"),
          add.lines = list(c("District * MC Party FEs", "Y", "Y", "Y", "Y"),
                           c("Congressional session FEs", "Y", "Y", "Y", "Y")), 
          dep.var.labels=c("MC approval"), font.size="small", omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          column.sep.width = "-3pt", 
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(0.1, .05, .01, .001),
          notes.align = "l",
          #label = "tab:dems-rr",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01; ***p < 0.001"), notes.append = F)

# interact gender congruence and hs with district-level controls for White Dems 
mod_hs1_fd <- felm(approval_rep ~ mc_gender + hs_index:mc_gender + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data=df_even_fd, weights=df_even_fd$weight)

# interact gender congruence and hs with district-level controls for POC Dems 
mod_hs2_md <- felm(approval_rep ~ mc_gender + hs_index:mc_gender + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data=df_even_md, weights=df_even_md$weight)

# interact gender congruence and hs with district-level controls for White GOP 
mod_hs3_fr <- felm(approval_rep ~ mc_gender + hs_index:mc_gender + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data=df_even_fr, weights=df_even_fr$weight)

# interact gender congruence and hs with district-level controls for POC GOP 
mod_hs4_mr <- felm(approval_rep ~ mc_gender + hs_index:mc_gender + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data=df_even_mr, weights=df_even_mr$weight)


# table
stargazer(mod_hs2_md, mod_hs1_fd, mod_hs4_mr, mod_hs3_fr, type="latex", 
          column.labels=c("\\shortstack{Democrat \\\\ Man}", "\\shortstack{Democrat \\\\ Woman}", "\\shortstack{Republican \\\\ Man}", "\\shortstack{Republican \\\\ Woman}"), 
          model.numbers=F, style="apsr",
          header=F, title = "Effects of Gender Congruence on MC Approval among D and R Respondents, Interacting MC Gender with Hostile Sexism",
          covariate.labels=c("Woman MC = 1","MC Seniority","POC MC", "HS | Woman MC = 1"),
          add.lines = list(c("District * MC Party FEs", "Y", "Y", "Y", "Y"),
                           c("Congressional session FEs", "Y", "Y", "Y", "Y")), 
          dep.var.labels=c("MC approval"), font.size="small", omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          column.sep.width = "-3pt", 
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(0.1, .05, .01, .001),
          notes.align = "l",
          #label = "tab:dems-rr",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01; ***p < 0.001"), notes.append = F)

## O. Leaners ----
# No Leaners D
df_even_no.lean.d <- df_even2 %>%
  filter(party3 == 1)
# Leaners D
df_even_lean.d <- df_even2 %>%
  filter(party7 == 1)
# No Leaners R
df_even_no.lean.r <- df_even2 %>%
  filter(party3 == 3)
# Leaners R
df_even_lean.r <- df_even2 %>%
  filter(party7 == 3)

## Basic approval for gender
# Includes leaners D
mod1.lean_d <- felm(approval_rep ~ gender_congruence + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | 
                      state_dist, data = df_even_lean.d, weights = df_even_lean.d$weight)
# Excludes leaners D
mod1.nolean_d <- felm(approval_rep ~ gender_congruence + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | 
                        state_dist, data = df_even_no.lean.d, weights = df_even_no.lean.d$weight)
# Includes leaners R
mod1.lean_r <- felm(approval_rep ~ gender_congruence + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | 
                      state_dist, data = df_even_lean.r, weights = df_even_lean.r$weight)
# Excludes leaners R
mod1.nolean_r <- felm(approval_rep ~ gender_congruence + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | 
                        state_dist, data = df_even_no.lean.r, weights = df_even_no.lean.r$weight)

## Perceived ideology for gender 
mod3_p.ideo.lean_d <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_lean.d, weights = df_even_lean.d$weight)

mod3_p.ideo.nolean_d <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_no.lean.d, weights = df_even_no.lean.d$weight)

mod3_p.ideo.lean_r <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_lean.r, weights = df_even_lean.r$weight)

mod3_p.ideo.nolean_r <- felm(approval_rep ~ gender_congruence.fac + inc_tercile_ideo2:gender_congruence.fac + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | state_dist, data = df_even_no.lean.r, weights = df_even_no.lean.r$weight)


## Basic approval for race
# Includes leaners D
mod1.lean_d.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                         state_dist, data = df_even_lean.d, weights = df_even_lean.d$weight)
# Excludes leaners D
mod1.nolean_d.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                           state_dist, data = df_even_no.lean.d, weights = df_even_no.lean.d$weight)
# Includes leaners R
mod1.lean_r.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                         state_dist, data = df_even_lean.r, weights = df_even_lean.r$weight)
# Excludes leaners R
mod1.nolean_r.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                           state_dist, data = df_even_no.lean.r, weights = df_even_no.lean.r$weight)

## Perceived ideology for race 
mod3_p.ideo.lean_d.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_lean.d, weights = df_even_lean.d$weight)

mod3_p.ideo.nolean_d.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_no.lean.d, weights = df_even_no.lean.d$weight)

mod3_p.ideo.lean_r.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_lean.r, weights = df_even_lean.r$weight)

mod3_p.ideo.nolean_r.nw <- felm(approval_rep ~ race_congruence.fac + inc_tercile_ideo2:race_congruence.fac + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_no.lean.r, weights = df_even_no.lean.r$weight)

# Create data frames with regression output of interest
# Gender first 
# Leaners D
mod_gen_p.ideo.lean.d <- broom::tidy(mod3_p.ideo.lean_d) %>% filter(term%in%c("gender_congruence.fac0:inc_tercile_ideo23", "gender_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "Leaners")
# No Leaners D
mod_gen_p.ideo.nolean.d <- broom::tidy(mod3_p.ideo.nolean_d) %>% filter(term%in%c("gender_congruence.fac0:inc_tercile_ideo23", "gender_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "No Leaners")
# Leaners R
mod_gen_p.ideo.lean.r <- broom::tidy(mod3_p.ideo.lean_r) %>% filter(term%in%c("gender_congruence.fac0:inc_tercile_ideo23", "gender_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "Leaners")
# No Leaners R
mod_gen_p.ideo.nolean.r <- broom::tidy(mod3_p.ideo.nolean_r) %>% filter(term%in%c("gender_congruence.fac0:inc_tercile_ideo23", "gender_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "No Leaners")

# Race Second 
# Leaners D
mod_gen_p.ideo.lean.d.nw <- broom::tidy(mod3_p.ideo.lean_d.nw) %>% filter(term%in%c("race_congruence.fac0:inc_tercile_ideo23", "race_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "Leaners")
# No Leaners D
mod_gen_p.ideo.nolean.d.nw <- broom::tidy(mod3_p.ideo.nolean_d.nw) %>% filter(term%in%c("race_congruence.fac0:inc_tercile_ideo23", "race_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "No Leaners")
# Leaners R
mod_gen_p.ideo.lean.r.nw <- broom::tidy(mod3_p.ideo.lean_r.nw) %>% filter(term%in%c("race_congruence.fac0:inc_tercile_ideo23", "race_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "Leaners")
# No Leaners R
mod_gen_p.ideo.nolean.r.nw <- broom::tidy(mod3_p.ideo.nolean_r.nw) %>% filter(term%in%c("race_congruence.fac0:inc_tercile_ideo23", "race_congruence.fac1:inc_tercile_ideo23")) %>% mutate(model = "No Leaners")

# Create tibble with all regression output for Democratic women
term <- c("Identity Incongruent", "Identity Congruent", "Identity Incongruent", "Identity Congruent")

# gender 
models.lean.d <- rbind(mod_gen_p.ideo.lean.d) %>% select(-term)
models.lean.d <- cbind(models.lean.d, term) %>% mutate(party = "Democrats")
models.nolean.d <- rbind(mod_gen_p.ideo.nolean.d) %>% select(-term)
models.nolean.d <- cbind(models.nolean.d, term) %>% mutate(party = "Democrats")
models.lean.r <- rbind(mod_gen_p.ideo.lean.r) %>% select(-term)
models.lean.r <- cbind(models.lean.r, term) %>% mutate(party = "Republicans")
models.nolean.r <- rbind(mod_gen_p.ideo.nolean.r) %>% select(-term)
models.nolean.r <- cbind(models.nolean.r, term) %>% mutate(party = "Republicans")
# race 
models.lean.d.nw <- rbind(mod_gen_p.ideo.lean.d.nw) %>% select(-term)
models.lean.d.nw <- cbind(models.lean.d.nw, term) %>% mutate(party = "Democrats")
models.nolean.d.nw <- rbind(mod_gen_p.ideo.nolean.d.nw) %>% select(-term)
models.nolean.d.nw <- cbind(models.nolean.d.nw, term) %>% mutate(party = "Democrats")
models.lean.r.nw <- rbind(mod_gen_p.ideo.lean.r.nw) %>% select(-term)
models.lean.r.nw <- cbind(models.lean.r.nw, term) %>% mutate(party = "Republicans")
models.nolean.r.nw <- rbind(mod_gen_p.ideo.nolean.r.nw) %>% select(-term)
models.nolean.r.nw <- cbind(models.nolean.r.nw, term) %>% mutate(party = "Republicans")

# Need to bind the datasets 
models.lean.d <- models.lean.d %>% mutate(lean = "Includes Leaners",
                                          party = "Democratic Respondents",
                                          identity = "Gender Congruence")
models.nolean.d <- models.nolean.d %>% mutate(lean = "Excludes Leaners",
                                              party = "Democratic Respondents",
                                              identity = "Gender Congruence")
models.lean.r <- models.lean.r %>% mutate(lean = "Includes Leaners",
                                          party = "Republican Respondents",
                                          identity = "Gender Congruence")
models.nolean.r <- models.nolean.r %>% mutate(lean = "Excludes Leaners",
                                              party = "Republican Respondents",
                                              identity = "Gender Congruence")
models.lean.d.nw <- models.lean.d.nw %>% mutate(lean = "Includes Leaners",
                                                party = "Democratic Respondents",
                                                identity = "Racical Congruence")
models.nolean.d.nw <- models.nolean.d.nw %>% mutate(lean = "Excludes Leaners",
                                                    party = "Democratic Respondents",
                                                    identity = "Racical Congruence")
models.lean.r.nw <- models.lean.r.nw %>% mutate(lean = "Includes Leaners",
                                                party = "Republican Respondents",
                                                identity = "Racical Congruence")
models.nolean.r.nw <- models.nolean.r.nw %>% mutate(lean = "Excludes Leaners",
                                                    party = "Republican Respondents",
                                                    identity = "Racical Congruence")

models.leaners <- rbind(models.lean.d, models.nolean.d, models.lean.r, models.nolean.r,
                        models.lean.d.nw, models.nolean.d.nw, models.lean.r.nw, models.nolean.r.nw)

models.leaners$identity <- factor(models.leaners$identity, levels = c("Gender Congruence", "Racical Congruence"))

dwplot(models.leaners,
       vline = geom_vline(
         xintercept = 0,
         colour = "grey60",
         linetype = 2
       ),
       # plot line at zero _behind_ coefs
       dot_args = list(aes(shape = model), size=1.5),
       whisker_args = list(aes(color = model))) +
  theme_bw() + ylab("") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
        axis.text.y = element_text(size = 8),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10))+
  scale_colour_manual(values = c("grey68", "gray35", "black")) +
  scale_y_discrete(limits=rev) +
  xlab("Marginal Effect of Ideological Incongruence on Approval") +
  coord_flip() +
  facet_nested(identity ~ party) +
  guides(shape = guide_legend("model"), 
         colour = guide_legend("model"))


## P. Don’t Know and Skipped Responses ---- 
## Basic approval for gender 
# Female D respondents 
mod2 <- felm(approval_rep ~ gender_congruence + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_fd, weights = df_even_fd$weight)
mod2.na <- felm(approval_rep_na ~ gender_congruence + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | 
                  state_dist, data = df_even_fd, weights = df_even_fd$weight)
mod2.mean <- felm(approval_rep_mean ~ gender_congruence + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | 
                    state_dist, data = df_even_fd, weights = df_even_fd$weight)
# Male D respondents 
mod3 <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_md, weights = df_even_md$weight)
mod3.na <- felm(approval_rep_na ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
                  state_dist, data = df_even_md, weights = df_even_md$weight)
mod3.mean <- felm(approval_rep_mean ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
                    state_dist, data = df_even_md, weights = df_even_md$weight)
# Female R respondents 
mod4 <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_fr, weights = df_even_fr$weight)
mod4.na <- felm(approval_rep_na ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
                  state_dist, data = df_even_fr, weights = df_even_fr$weight)
mod4.mean <- felm(approval_rep_mean ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
                    state_dist, data = df_even_fr, weights = df_even_fr$weight)
# Male R respondents 
mod5 <- felm(approval_rep ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
               state_dist, data = df_even_mr, weights = df_even_mr$weight)
mod5.na <- felm(approval_rep_na ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
                  state_dist, data = df_even_mr, weights = df_even_mr$weight)
mod5.mean <- felm(approval_rep_mean ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
                    state_dist, data = df_even_mr, weights = df_even_mr$weight)


## Basic approval for Race 
# Nonwhite Democrats 
mod2.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                  state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
mod2.nw.na <- felm(approval_rep_na ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                     state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
mod2.nw.mean <- felm(approval_rep_mean ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                       state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
# White Democrats 
mod3.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                  state_dist, data = df_even_wd, weights = df_even_wd$weight)
mod3.nw.na <- felm(approval_rep_na ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                     state_dist, data = df_even_wd, weights = df_even_wd$weight)
mod3.nw.mean <- felm(approval_rep_mean ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                       state_dist, data = df_even_wd, weights = df_even_wd$weight)
# Nonwhite Republicans
mod4.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                  state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
mod4.nw.na <- felm(approval_rep_na ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                     state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
mod4.nw.mean <- felm(approval_rep_mean ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                       state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
# White Republicans 
mod5.nw <- felm(approval_rep ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                  state_dist, data = df_even_wr, weights = df_even_wr$weight)
mod5.nw.na <- felm(approval_rep_na ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                     state_dist, data = df_even_wr, weights = df_even_wr$weight)
mod5.nw.mean <- felm(approval_rep_mean ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | 
                       state_dist, data = df_even_wr, weights = df_even_wr$weight)

# Create data frames with regression output of interest
# Gender 
# Democratic women 
mod_gen <- broom::tidy(mod2) %>% filter(term%in%c("gender_congruence")) %>% mutate(model = "Women Respondents")
mod_gen_na <- broom::tidy(mod2.na) %>% filter(term%in%c("gender_congruence")) %>% mutate(model = "Women Respondents")
mod_gen_mean <- broom::tidy(mod2.mean) %>% filter(term%in%c("gender_congruence")) %>% mutate(model = "Women Respondents")
# Democratic men 
mod2_gen <- broom::tidy(mod3) %>% filter(term%in%c("gender_congruence")) %>% mutate(model = "Men Respondents")
mod2_gen_na <- broom::tidy(mod3.na) %>% filter(term%in%c("gender_congruence")) %>% mutate(model = "Men Respondents")
mod2_gen_mean <- broom::tidy(mod3.mean) %>% filter(term%in%c("gender_congruence")) %>% mutate(model = "Men Respondents")
# Republican women
mod3_gen <- broom::tidy(mod4) %>% filter(term%in%c("gender_congruence")) %>% mutate(model = "Women Respondents")
mod3_gen_na <- broom::tidy(mod4.na) %>% filter(term%in%c("gender_congruence")) %>% mutate(model = "Women Respondents")
mod3_gen_mean <- broom::tidy(mod4.mean) %>% filter(term%in%c("gender_congruence")) %>% mutate(model = "Women Respondents")
# Republican men
mod4_gen <- broom::tidy(mod5) %>% filter(term%in%c("gender_congruence")) %>% mutate(model = "Men Respondents")
mod4_gen_na <- broom::tidy(mod5.na) %>% filter(term%in%c("gender_congruence")) %>% mutate(model = "Men Respondents")
mod4_gen_mean <- broom::tidy(mod5.mean) %>% filter(term%in%c("gender_congruence")) %>% mutate(model = "Men Respondents")

# Race 
# Nonwhite Democrats 
mod_nw <- broom::tidy(mod2.nw) %>% filter(term%in%c("race_congruence")) %>% mutate(model = "POC Respondents")
mod_nw_na <- broom::tidy(mod2.nw.na) %>% filter(term%in%c("race_congruence")) %>% mutate(model = "POC Respondents")
mod_nw_mean <- broom::tidy(mod2.nw.mean) %>% filter(term%in%c("race_congruence")) %>% mutate(model = "POC Respondents")
# White Republicans 
mod2_nw <- broom::tidy(mod3.nw) %>% filter(term%in%c("race_congruence")) %>% mutate(model = "White Respondents")
mod2_nw_na <- broom::tidy(mod3.nw.na) %>% filter(term%in%c("race_congruence")) %>% mutate(model = "White Respondents")
mod2_nw_mean <- broom::tidy(mod3.nw.mean) %>% filter(term%in%c("race_congruence")) %>% mutate(model = "White Respondents")
# Nonwhite Democrats
mod3_nw <- broom::tidy(mod4.nw) %>% filter(term%in%c("race_congruence")) %>% mutate(model = "POC Respondents")
mod3_nw_na <- broom::tidy(mod4.nw.na) %>% filter(term%in%c("race_congruence")) %>% mutate(model = "POC Respondents")
mod3_nw_mean <- broom::tidy(mod4.nw.mean) %>% filter(term%in%c("race_congruence")) %>% mutate(model = "POC Respondents")
# White Democrats
mod4_nw <- broom::tidy(mod5.nw) %>% filter(term%in%c("race_congruence")) %>% mutate(model = "White Respondents")
mod4_nw_na <- broom::tidy(mod5.nw.na) %>% filter(term%in%c("race_congruence")) %>% mutate(model = "White Respondents")
mod4_nw_mean <- broom::tidy(mod5.nw.mean) %>% filter(term%in%c("race_congruence")) %>% mutate(model = "White Respondents")

# Create tibble with all regression output for Democratic women
term <- c("DK as Median", "DK as NA", "DK as Mean")

models1_gen <- rbind(mod_gen, mod_gen_na, mod_gen_mean) %>% select(-term)
models1_gen <- cbind(models1_gen, term)
models2_gen <- rbind(mod2_gen, mod2_gen_na, mod2_gen_mean) %>% select(-term)
models2_gen <- cbind(models2_gen, term)
models3_gen <- rbind(mod3_gen, mod3_gen_na, mod3_gen_mean) %>% select(-term)
models3_gen <- cbind(models3_gen, term)
models4_gen <- rbind(mod4_gen, mod4_gen_na, mod4_gen_mean) %>% select(-term)
models4_gen <- cbind(models4_gen, term)

models1_nw <- rbind(mod_nw, mod_nw_na, mod_nw_mean) %>% select(-term)
models1_nw <- cbind(models1_nw, term)
models2_nw <- rbind(mod2_nw, mod2_nw_na, mod2_nw_mean) %>% select(-term)
models2_nw <- cbind(models2_nw, term)
models3_nw <- rbind(mod3_nw, mod3_nw_na, mod3_nw_mean) %>% select(-term)
models3_nw <- cbind(models3_nw, term)
models4_nw <- rbind(mod4_nw, mod4_nw_na, mod4_nw_mean) %>% select(-term)
models4_nw <- cbind(models4_nw, term)

models1_gen <- models1_gen %>% mutate(party = "Democrat",
                                      identity = "Women Respondents")
models2_gen <- models2_gen %>% mutate(party = "Democrat",
                                      identity = "Men Respondents")
models3_gen <- models3_gen %>% mutate(party = "Republican",
                                      identity = "Women Respondents")
models4_gen <- models4_gen %>% mutate(party = "Republican",
                                      identity = "Men Respondents")
models1_nw <- models1_nw %>% mutate(party = "Democrat",
                                    identity = "POC Respondents")
models2_nw <- models2_nw %>% mutate(party = "Democrat",
                                    identity = "White Respondents")
models3_nw <- models3_nw %>% mutate(party = "Republican",
                                    identity = "POC Respondents")
models4_nw <- models4_nw %>% mutate(party = "Republican",
                                    identity = "White Respondents")

models.gender.dk <- rbind(models1_gen, models2_gen, models3_gen, models4_gen)
models.race.dk <- rbind(models1_nw, models2_nw, models3_nw, models4_nw)

## Combining Race and Gender into one figure  
models.gender.dk <- models.gender.dk %>% mutate(type = "Gender Congruence")
models.race.dk <- models.race.dk %>% mutate(type = "Racial Congruence")

both.models.dk <- rbind(models.gender.dk, models.race.dk)

both.models.dk$identity <- factor(both.models.dk$identity, 
                                  levels = c("Women Respondents", "Men Respondents",
                                             "POC Respondents", "White Respondents"))


dwplot(both.models.dk,
       vline = geom_vline(xintercept = 0,
                          colour = "grey60",
                          linetype = 2),
  # plot line at zero _behind_ coefs
  dot_args = list(aes(shape = model), size=2),
  whisker_args = list(aes(color = model))) +
  theme_bw() + xlab("") + ylab("") +
  #ggtitle("Basic Model with 'Don't Know' for Approval Coded as Median, NA, Mean") +
  theme(legend.title = element_blank(),
        plot.title = element_text(size = 12, face = "bold", hjust = 0.5),
        axis.text.y = element_text(size = 8),
        legend.position = "bottom",
        legend.margin=margin(0),
        legend.box.margin=margin(-10))+
  scale_colour_manual(values = c("grey68", "gray35", "grey" ,"black")) +
  xlab("Effect of Identity Congruence on MC Approval") +
  xlim(-.07, .12) +
  scale_y_discrete(limits=rev) +
  coord_flip() +
  facet_wrap(type ~ party) +
  guides(shape = guide_legend("model"), 
         colour = guide_legend("model"))

#

## Q. Electoral preferences ----

# DV vote_winner: whether they voted for the sitting MC or against the sitting MC 

# Female D respondents 
mod.vote1 <- felm(vote_winner ~ gender_congruence + mc_seniority + nonwhite_mc | cong + dist_party_mc | 0 | 
                    state_dist, data = df_even_fd, weights = df_even_fd$weight) 

# Male D respondents 
mod.vote2 <- felm(vote_winner ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
                    state_dist, data = df_even_md, weights = df_even_md$weight)
# Female R respondents 
mod.vote3 <- felm(vote_winner ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
                    state_dist, data = df_even_fr, weights = df_even_fr$weight)
# Male R respondents 
mod.vote4 <- felm(vote_winner ~ gender_congruence + mc_seniority  + nonwhite_mc | cong + dist_party_mc | 0 | 
                    state_dist, data = df_even_mr, weights = df_even_mr$weight)

treat2 <- df_even_fd %>%
  filter(!is.na(state_dist)&!is.na(vote_winner)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod2_t <- length(which(treat2$treated==1))

treat3 <- df_even_md %>%
  filter(!is.na(state_dist)&!is.na(vote_winner)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod3_t <- length(which(treat3$treated==1))

treat4 <- df_even_fr %>%
  filter(!is.na(state_dist)&!is.na(vote_winner)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod4_t <- length(which(treat4$treated==1))

treat5 <- df_even_mr %>%
  filter(!is.na(state_dist)&!is.na(vote_winner)&!is.na(mc_gender)) %>%
  select(state_dist, mc_gender, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_gender = mean(mc_gender, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_gender > 0 & pct_gender < 1 ~ 1, TRUE ~ 0))
mod5_t <- length(which(treat5$treated==1))

stargazer(mod.vote1, mod.vote2, mod.vote3, mod.vote4,
          omit = c('[i][n][d]'), type='latex', header=FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{(1) D Women \\\\ Respondents}", "\\shortstack{(2) D Men \\\\ Respondents}", 
                            "\\shortstack{(3) R Women \\\\ Respondents}", "\\shortstack{(4) R Men \\\\ Respondents}"),
          covariate.labels=c("Gender Congruence = 1", "MC Seniority", "MC Race"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X'),
                         c("N districts w MC gender change", mod2_t, mod3_t, mod4_t, mod5_t)),
          title = "Effect of Gender Congruence on Whether a respondent voted for their MC",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:gender",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001", 
                    "ADD NOTE"), notes.append = F)

# RACE
# Nonwhite D respondents 
mod.vote1.nw <- felm(vote_winner ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwd, weights = df_even_nwd$weight)
# White D respondents 
mod.vote2.nw <- felm(vote_winner ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wd, weights = df_even_wd$weight)
# Nonwhite R respondents 
mod.vote3.nw <- felm(vote_winner ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_nwr, weights = df_even_nwr$weight)
# White R respondents 
mod.vote4.nw <- felm(vote_winner ~ race_congruence + mc_seniority + mc_gender | cong + dist_party_mc | 0 | state_dist, data = df_even_wr, weights = df_even_wr$weight)

treat2.nw <- df_even_nwd %>%
  filter(!is.na(state_dist)&!is.na(vote_winner)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod2_t.nw <- length(which(treat2.nw$treated==1))

treat3.nw <- df_even_wd %>%
  filter(!is.na(state_dist)&!is.na(vote_winner)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod3_t.nw <- length(which(treat3.nw$treated==1))

treat4.nw <- df_even_nwr %>%
  filter(!is.na(state_dist)&!is.na(vote_winner)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod4_t.nw <- length(which(treat4.nw$treated==1))

treat5.nw <- df_even_wr %>%
  filter(!is.na(state_dist)&!is.na(vote_winner)&!is.na(nonwhite_mc)) %>%
  select(state_dist, nonwhite_mc, year, mc_party) %>%
  distinct() %>%
  group_by(state_dist, mc_party) %>%
  summarise(pct_nw = mean(nonwhite_mc, na.rm=T)) %>%
  ungroup() %>%
  mutate(treated = case_when(pct_nw > 0 & pct_nw < 1 ~ 1, TRUE ~ 0))
mod5_t.nw <- length(which(treat5.nw$treated==1))

stargazer(mod.vote1.nw, mod.vote2.nw, mod.vote3.nw, mod.vote4.nw,
          omit = c('[i][n][d]'), type='latex', header=FALSE, style="apsr",
          column.sep.width = ".5pt", font.size="scriptsize",
          model.names	= FALSE, colnames = FALSE, model.numbers = FALSE, 
          dep.var.labels=c(""),
          column.labels = c("\\shortstack{(1) D POC \\\\ Respondents}", "\\shortstack{(2) D White \\\\ Respondents}", 
                            "\\shortstack{(3) R POC \\\\ Respondents}", "\\shortstack{(4) R White \\\\ Respondents}"),
          covariate.labels=c("Racial Congruence = 1", "MC Seniority", "MC Gender"),
          omit.stat=c("f", "ser", "adj.rsq", "rsq"),
          add.lines=list(c('Congress FE', 'X','X','X','X'),
                         c('MC Party x Dist FE', 'X','X','X','X'),
                         c("N districts w MC race change", mod2_t.nw, mod3_t.nw, mod4_t.nw, mod5_t.nw)),
          title = "Effect of Racial Congruence on Whether a respondent voted for their MC",
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          label = "tab:POC",
          notes.align = "l",
          notes = c("+p < 0.1; *p < 0.05; **p < 0.01, ***p < 0.001", 
                    "NOTE HERE"), notes.append = F)


